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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14546347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:14:44  来源:igfitidea点击:

How to use nested class in WPF XAML?

c#wpfxamldata-bindingnested-class

提问by Gennady Vanin Геннадий Ванин

I am refactoring the code from sample:

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

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

在排除 Skills class 后
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:

  • Skillclass into Employeeclass
  • Skill上课Employee上课

in Window1.xaml.cs?

在 Window1.xaml.cs 中?

Related questions

相关问题

based on the same code:

基于相同的代码:

Update (answering 1st RV1987's comment):

更新(回答 1st RV1987 的评论)

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

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

  • answer by Ludovictells 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 townseanis based on citation from msdn:
    "Your custom class must not be a nested class. Nested classes and the "dot"in their generalCLR 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 concretecode 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.

  • Ludovic 的回答说这是可能的,但包含不清楚如何使用的评论。
    这很符合我的经验和这个问题
  • Townsean 的另一个答案是基于msdn 的引用:
    “您的自定义类不能是嵌套类。嵌套类和其一般CLR 使用语法中的“点”会干扰其他 WPF 和/或 XAML 功能,例如附加属性。”

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

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

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

  • 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
  • 即使在完美工作的元素上,XAML 也会给我错误
  • 我没有尝试过自己使用反射器,也没有从微软那里找到任何其他可行的方法或不那么模棱两可的参考

回答by Cédric Bignon

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 用法语法中的“点”会干扰其他 WPF 和/或 XAML 功能,例如附加属性。