如何更改WPF XAML以反映C#代码中的更改? [英] How to change the WPF XAML to reflect changes in the C# code-behind?
问题描述
我从样本中折叠代码:
- 24.129.21。主要细节绑定
从 C#/ CSharp教程»Windows Presentation Foundation»Binding)
- 24.129.21. Master Detail Binding
from C# / CSharp Tutorial » Windows Presentation Foundation » Binding)
我来到了以下C#代码隐藏,运行同样的XAML: p>
I came to the following C# code-behind, running with the same XAML:
namespace WpfApplication1
{
public class Skill
{
public string Description { get; set; }
}
//I'd like to comment out the next line
public class Skills : ObservableCollection<Skill>{ }
public class Employee
{
public string Name { get ; set; }
public int Age { get; set; }
//public Skills Skills { get; set; }
//instead of line above - works!
public ObservableCollection<Skill> Skills { 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 MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
从
-
公开技能技能{get;组; $ $>
to
-
public ObservableCollection< Skill>技能{get;组;
public ObservableCollection<Skill> Skills { get; set; }
在 Employee
class
因此,我想摆脱,即注释掉这一行:
Consequently, I'd like to get rid of, i.e. to comment out the line:
public class Skills : ObservableCollection<Skill>{ }
但我得到: / p>
but I'm getting:
错误1标签技能不存在于XML命名空间
'clr-namespace中: _24_129_21_Master_Detail_Binding。第13行位置34
Error 1 The tag 'Skills' does not exist in XML namespace 'clr-namespace:_24_129_21_Master_Detail_Binding'. Line 13 Position 34
在 Window1.xaml
<local:Team>
<local:Employee Name="Larry" Age="21">
<local:Employee.Skills>
<local:Skills>
如何更改XAML才能做到?
How can I change XAML in order to do it?
如何更改XAML?
推荐答案
在您的 Window1.xaml
类中删除对技能的引用类:(例如< local:Skills>
),因为你没有技能类。相反,您需要在您的ObservableCollection内部获取一些技能
元素:
In your Window1.xaml
class remove the reference to the skills class: (e.g. <local:Skills>
) since you don't have a Skills class anymore. Instead you want a number of Skill
elements inside your ObservableCollection:
<local:Team>
<local:Employee Name="Larry" Age="21">
<local:Employee.Skills>
<local:Skill />
<local:Skill />
....
此外,您应该可以初始化技能 ObservableCollection在Employee的构造函数内。
Also, you should probably initialize the Skills ObservableCollection inside the Employee's constructor.
这篇关于如何更改WPF XAML以反映C#代码中的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!