Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

c# - How to use nested class in WPF XAML?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...