如何在WPF XAML中使用嵌套类?

时间:2022-11-25 08:28:35

I am refactoring the code from sample:

我正在重构示例中的代码:

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

排除技能类后,在MainWindow.xaml中进行相应的更改。

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

in MainWindow1.xaml.cs:

在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:

我该如何更改Window1。XAML如果移动:

  • Skill class into Employee class
  • 技能课程进入员工课程

in Window1.xaml.cs?

在Window1.xaml.cs吗?

Related questions

based on the same code:

基于相同的代码:

Update (answering 1st RV1987's comment):

更新(回答1987年第一次裂谷热的评论):

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

在XAML中创建嵌套类实例的回答告诉我们可以使用,但是不清楚如何使用:

  • 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."

    townsean的另一个答案基于msdn的引用:“您的自定义类不能是嵌套类。嵌套类和一般CLR使用语法中的“dot”会干扰其他WPF和/或XAML特性,如附加属性。

    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.

    但是,一般来说,对于“您的自定义类”,在我附加到这个问题的具体代码中,有几十个“点”(比如Employee.Skills),而且嵌套的不是我的自定义类,而是我的自定义类。

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

Update2(回答第2个RV1987的评论问题):是的,我刚刚尝试过+方法,它不起作用,但是:

  • XAML gives me errors even on perfectly working elements
  • XAML即使在完美的工作元素上也会出错
  • I've not tried to use reflector myself or find any other workable approach or less ambiguous reference from Microsoft on it
  • 我自己也没有尝试使用reflector,也没有找到任何其他可行的方法,也没有找到微软在这方面不太明确的参考

1 个解决方案

#1


35  

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

不幸的是,在XAML(来自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.

您的自定义类必须不是嵌套类。嵌套类和一般CLR使用语法中的“dot”会干扰其他WPF和/或XAML特性,如附加属性。

#1


35  

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

不幸的是,在XAML(来自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.

您的自定义类必须不是嵌套类。嵌套类和一般CLR使用语法中的“dot”会干扰其他WPF和/或XAML特性,如附加属性。