Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use nested class in WPF XAML?

I am refactoring the code from sample:

  • 24.129.21. Master Detail Binding
    from C# / CSharp Tutorial » Windows Presentation Foundation » Binding)

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:

  • How to change a custom type to System type (string) in WPF XAML?

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
like image 544

1 Answers

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.

like image 154
Cédric Bignon Avatar answered Oct 14 '22 13:10

Cédric Bignon