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

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

问题描述

我从样本重构code:

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 class into Employee class

在Window1.xaml.cs?

in Window1.xaml.cs?

基于相同的code:

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

更新(回答第一个RV1987的评论)

解答TP <一个href=\"http://stackoverflow.com/questions/4269896/creating-an-instance-of-a-nested-class-in-xaml\">Creating在XAML嵌套类的实例告诉,这是可能的,但目前还不清楚如何使用:

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


  • 由朱利答案告诉,这是可能的,但包含评论说,目前尚不清楚怎么用。结果
    这是很符合我的经验线,这个问题

  • 由townsean 另一答案是基于引文的from 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."

但是,它是在常规,然后在自定义类,但在我的混凝土 $ C $连接到这个问题ç有几十(如 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(回答第二个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天全站免登陆