如何在 WPF XAML 中使用嵌套类? [英] How to use nested class in WPF XAML?

查看:51
本文介绍了如何在 WPF XAML 中使用嵌套类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am refactoring the code from sample:

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

排除技能类后,相应的变化
MainWindow.xaml

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

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();
    }
  }
}

如果要移动,我应该如何更改 Window1.XAML:

How should I change Window1.XAML if to move:

  • Skill类转化为Employee

在 Window1.xaml.cs 中?

in Window1.xaml.cs?

基于相同的代码:

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

Answers tp 在 XAML 中创建嵌套类的实例 告诉它可以但不清楚如何使用:

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

  • Ludovic 的回答 说这是可能的,但包含不清楚如何使用的评论.
    这很符合我的经验和这个问题
  • 另一个 townsean 的回答 基于引用 来自 msdn:
    您的自定义类不能是嵌套类.嵌套类和一般CLR 用法语法中的点" 会干扰其他 WPF 和/或 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."

但是,它是一般,对于您的自定义类",但在我的具体代码中附加到这个问题有几十个点"(如 Employee.Skills),它不是嵌套的自定义类,而是我的自定义类在其中嵌套了类.

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(回答 2nd RV1987 的评论问题):
是的,我刚刚尝试了 + 方法,但不起作用,但是:

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

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

推荐答案

不幸的是,您想要做的在 XAML 中是不可能的(来自 MSDN):

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

您的自定义类不能是嵌套类.嵌套类和通用 CLR 用法语法中的点"会干扰其他 WPF 和/或 XAML 功能,例如附加属性.

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.

这篇关于如何在 WPF XAML 中使用嵌套类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆