WPF数据绑定到复合类模式?

我是第一次尝试WPF,我正在努力解决如何将控件绑定到使用其他对象组合构建的类。 例如,如果我有由两个单独的类构成的类Comp(请注意为清晰起见省略了各种元素):

class One { int _first; int _second; } class Two { string _third; string _fourth; } class Comp { int _int1; One _part1; Two _part2; } 

现在我明白我可以使用Comp中定义的“get”轻松绑定_int1。 但是我如何绑定元素_part1._first,_part1._second。 我是否在Comp级别为他们公开了“getters”? 或者我可以在复合类中公开它们并使用指向它们的绑定路径吗? 这如何设置属性?

这是模式吗?

 ....  .... class One { int _first; int _second; } class Two { string _third; string _fourth; } class Comp { int _int1; One _part1; Two _part2; int Int1 { get { return _int1; } set { _int1 = value; } } int First { get { return _part1._first; } set { _part1._first = value; } } int Second { get { return _part1._second; } set { _part1._second = value; } } string Third { get { return _part2._third; } set { _part2._third = value; } } string Fourth { get { return _part2.fourth; } set { _part2._fourth = value; } } } ... Comp oComp = new Comp(); txtBlock.DataContext = oComp; ... 

或者这是模式? (我不知道该放什么路径)

 ....  .... class One { int _first; int _second; int First { get { return _first; } set { _first = value; } } int Second { get { return _second; } set { _second = value; } } } class Two { string _third; string _fourth; string Third { get { return _third; } set { _third = value; } } string Fourth { get { return _fourth; } set { _fourth = value; } } } class Comp { int _int1; One _part1; Two _part2; int Int1 { get { return _int1; } } } ... Comp oComp = new Comp(); txtBlock.DataContext = oComp; ... 

或者我正在努力重新发明MV-VM(我慢慢开始理解)?

 ....  .... class One { int _first; int _second; } class Two { string _third; string _fourth; } class Comp { int _int1; One _part1; Two _part2; } class CompView { Comp _comp; CompView( Comp comp ) { _comp = comp; } int Int1 { get { return _comp._int1; } set { _comp._int1 = value; } } int First { get { return _comp._part1._first; } set { _comp._part1._first = value; } } int Second { get { return _comp._part1._second; } set { _comp._part1._second = value; } } string Third { get { return _comp._part2._third; } set { _comp._part2._third = value; } } string Fourth { get { return _comp._part2.fourth; } set { _comp._part2._fourth = value; } } } ... Comp oComp = new Comp(); CompView oCompView = new CompView( oComp ); txtBlock.DataContext = oCompView; ... 

那我该怎么做呢? 如果它是第一个或第三个模式,那么我似乎已经把我所有可爱的(不同的)分层数据并将其压缩到平面配置,这样我就可以将它绑定到UI元素。 它是如何发生的,还是有更好的方法(第二种模式?)

编辑

我没有提出我真的想要双向绑定的问题。 所以属性访问器真的应该得到并设置。

编辑

更新了我的伪代码以显示setter以及getter

编辑

我按照马克和朱利安提供的模式进行了跟踪并实施了设置者,并对结果感到满意。 出于某种原因,我确信自己的财产设置不会一直到最终实体。

数据绑定通过属性工作,因此您不会在绑定中使用任何成员变量,例如:

 int _first public int First { get { return _first; } } 

你将使用First而不是_first作为绑定。 通常我已经看到每个类都提供它自己的属性来绑定,在这种情况下你可以修改你的代码:

 class One : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } int _first = 1; int _second = 2; public int First { get { return _first; } set { _first = value; OnPropertyChanged("First"); } } public int Second { get { return _second; } set { _second = value; OnPropertyChanged("Second"); } } } class Two : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } string _third = "Third"; string _fourth = "Fourth"; public string Third { get { return _third; } set { _third = value; OnPropertyChanged("Third"); } } public string Fourth { get { return _fourth; } set { _fourth = value; OnPropertyChanged("Fourth"); } } } class Comp : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } int _int1 = 100; One _part1 = new One(); Two _part2 = new Two(); public One Part1 { get { return _part1; } set { _part1 = value; OnPropertyChanged("Part1"); } } public Two Part2 { get { return _part2; } set { _part2 = value; OnPropertyChanged("Part2"); } } public int Int1 { get { return _int1; } set { _int1 = value; OnPropertyChanged("Int1"); } } } 

请注意,我将属性设为公开,同时将字段默认为私有。 如果将父控件的DataContext分配给Comp的实例:

 Comp comp = new Comp(); stack.DataContext = comp; 

然后,您可以按如下方式绑定到xaml中的片段:

          

在这里你会看到StackPanel被赋予一个Comp作为DataContext(因此它的所有子节点也都有DataContext,除非指定了另一个),并且文本绑定到它的成员类。

编辑:我还添加了setter以及实现了INotifyPropertyChanged,这是数据绑定的重要组成部分。 实现此function后,您将能够将数据绑定到多个控件,并在更新时查看所有控件中的数据更新。 你需要添加:using System.ComponentModel;

我认为你总是必须绑定到一个属性,所以你的类应该是:

 class One { int _first; int _second; int First { get { return _first; } } int Second { get { return _second; } } } class Two { string _third; string _fourth; string Third { get { return _third; } } string Fourth { get { return fourth; } } } class Comp { int _int1; One _part1; Two _part2; One Part1 { get { return _part1; } } Two Part2 { get { return _part2; } } } 

然后,你应该能够绑定到任何你想要的东西:

 ....  ....