25

I am refactoring the code from sample:

And after excluding Skills class, with corresponding changes in
in MainWindow.xaml

<local:Team>
  <local:Employee Name="Larry" Age="21">
    <local:Employee.Skills>
      <!--  local:Skills -->
       <local:Skills>

in MainWindow1.xaml.cs:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApplication
{
  public class Skill
  {//I'd like to exclude class Skill having moved it into class Employee as nested one
    public string Description { get; set; }
  }

   public class Employee 
   {
    public string Name { get  ; set; }
    public int Age  { get; set; }
    public List<Skill> Skills { get; set; }

     public Employee()
     {
       Skills=new List<Skill>();
     }

     /*class Skill  
     {
          public string Description { get; set; }
     }   */
  }

  public class Team : ObservableCollection<Employee> { }

  public class Company
  {
    public string CompanyName { get  ; set; }
    public Team Members { get  ; set; }
  }

  public class Companies : ObservableCollection<Company> { }

  public partial class Window1 : Window
    {
      public Window1()
    {
        InitializeComponent();
    }
  }
}

How should I change Window1.XAML if to move:

  • Skill class into Employee class

in Window1.xaml.cs?

Related questions

based on the same code:

Update (answering 1st RV1987's comment):

Answers tp Creating an instance of a nested class in XAML tell that it is possible but unclear how to use:

  • answer by Ludovic tells that it is possible but contains comment that it is not clear how to use.
    This is quite in line with my experience and this question
  • another answer by townsean is based on citation from msdn:
    "Your custom class must not be a nested class. Nested classes and the "dot" in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties."

    But, it is in general, and for "your custom class" but in in my concrete code attached to this question there are dozens "dots" (like Employee.Skills) and it is not my custom class that is nested but my custom class has nested class inside.

Update2 (answering 2nd RV1987's comment-question):
Yes, I've just tried that + approach, which does not work, but:

  • XAML gives me errors even on perfectly working elements
  • I've not tried to use reflector myself or find any other workable approach or less ambiguous reference from Microsoft on it
Community
  • 1
  • 1
  • 2
    Nested class object can't be created in xaml. Refer to this link - http://stackoverflow.com/questions/4269896/creating-an-instance-of-a-nested-class-in-xaml – Rohit Vats Jan 27 '13 at 10:26
  • Really, you link tells that it is possible. See my update – Gennady Vanin Геннадий Ванин Jan 27 '13 at 11:10
  • Have you tried that `+` approach? It will give parse error. – Rohit Vats Jan 27 '13 at 11:17
  • 1
    You want to have `` to populate your `` but `Skill` class is nested class in an `Employee` class. – Rohit Vats Jan 27 '13 at 11:21
  • See my "Related questions' in post for what I want. I want to eliminate Skill class, it has only one string property, having moved its Description prop into Employee class. I cannot grasp the sense of having class for just one string – Gennady Vanin Геннадий Ванин Jan 27 '13 at 11:28
  • Your question state you want to have `Skill` as nested class in your `Employee` class. So, how you eliminating the class here? – Rohit Vats Jan 27 '13 at 11:45
  • 1
    @GennadyVanin--ГеннадийВанин I think that what you are looking for is possible, but overly complicated. It's very easy to bind in XAML to nested properties using Employee.Skills.Description. For the creation and management of such entities, it's easier to use MVVM approach and create them in the ViewModel, in C# code. – Hannish Jan 27 '13 at 11:58

1 Answers1

46

Unfortunately, what you want to do is not possible in XAML (from MSDN):

Your custom class must not be a nested class. Nested classes and the "dot" in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties.

Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51