C# WPF使用类和子类绑定到DataContext

C# WPF使用类和子类绑定到DataContext,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我在wpf中玩数据绑定,遇到了一个问题。这是我的密码: main window.xaml <Window x:Class="TestWpf.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-nam

我在wpf中玩数据绑定,遇到了一个问题。这是我的密码:

main window.xaml

<Window x:Class="TestWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestWpf"
        Title="MainWindow" Height="350" Width="525">    
    <StackPanel Name="stackpanel">
        <TextBox Name="tb1" Text="{Binding Path=A.Number}" />
        <TextBox Name="tb2" Text="{Binding Path=B.Number}" />
        <TextBlock Name="tbResult" Text="{Binding Path=C}" />
    </StackPanel>
</Window>
MyClass.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyClass myClass = new MyClass();
        myClass.A = new MySubClass();
        myClass.B = new MySubClass();
        stackpanel.DataContext = myClass;
    }
}
class MyClass : INotifyPropertyChanged
{
    private MySubClass a;
    public MySubClass A 
    {
        get { return a; }
        set
        {
            a = value;
            OnPropertyChanged("A");
            OnPropertyChanged("C");
        }
    }

    private MySubClass b;
    public MySubClass B 
    {
        get { return b; }
        set
        {
            b = value;
            OnPropertyChanged("B");
            OnPropertyChanged("C");
        }
    }

    public int C
    {
        get { return A.Number + B.Number; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }        
}
class MySubClass : INotifyPropertyChanged
{
    private int number;
    public int Number 
    {
        get { return number; }
        set
        {
            number = value;
            OnPropertyChanged("Number");
        }
    }

    public MySubClass()
    {
        Number = 1;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }     
}
MySubClass.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MyClass myClass = new MyClass();
        myClass.A = new MySubClass();
        myClass.B = new MySubClass();
        stackpanel.DataContext = myClass;
    }
}
class MyClass : INotifyPropertyChanged
{
    private MySubClass a;
    public MySubClass A 
    {
        get { return a; }
        set
        {
            a = value;
            OnPropertyChanged("A");
            OnPropertyChanged("C");
        }
    }

    private MySubClass b;
    public MySubClass B 
    {
        get { return b; }
        set
        {
            b = value;
            OnPropertyChanged("B");
            OnPropertyChanged("C");
        }
    }

    public int C
    {
        get { return A.Number + B.Number; }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }        
}
class MySubClass : INotifyPropertyChanged
{
    private int number;
    public int Number 
    {
        get { return number; }
        set
        {
            number = value;
            OnPropertyChanged("Number");
        }
    }

    public MySubClass()
    {
        Number = 1;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string p)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }     
}

现在的问题是,在我运行应用程序后,绑定工作正常。此外,当我在文本框中更改值A.Number和B.Number时,它们的更新情况良好。但MyClass.C中的变量C只在应用程序启动时更新,以后不会更新。我需要更改什么,以便C在我更改A.编号或B.编号时更新。谢谢。

在更新数据模型时,您是直接更改MySubClass实例的编号,还是为MyClass实例分配新的子类?i、 e:

myClass.A.Number = 5; // will trigger OnPropertyChanged on MySubClass, but not on MyClass
不会在A上触发OnPropertyChanged(但当然会更新A.Number)。 要使其发挥作用,您必须执行以下操作:

MySubClass v = new MySubClass()
v.Number = 5;
myClass.A = v;  // will trigger OnPropertyChanged on MyClass

更新答案。您可以这样做以捕获a和b中的任何属性更改

public MyClass()
{
    a.PropertyChanged += new PropertyChangedEventHandler(UpdateC);
    b.PropertyChanged += new PropertyChangedEventHandler(UpdateC);
}

void UpdateC(object sender, PropertyChangedEventArgs e)
{
    OnPropertyChanged("C");
}
我需要更改什么,以便C在我更改A.编号或B.编号时更新。 您需要侦听A和B属性的更改。如果将此代码添加到setter,则应该可以使用:



set
{
if (a != null)
{
 a.PropertyChanged-=NumberChanged;
}
 a = value;
 a.PropertyChanged+=NumberChanged;
 OnPropertyChanged("A");
 OnPropertyChanged("C");
}

private void NumberChanged(object sender, PropertyChangedEventArgs e)
{
  if (e.PropertyName == "Number")
  {
    OnPropertyChanged("C");
  }
}



B setter也是如此。

谢谢您的回答,但是是否可以在不实例化MySubClass的情况下以某种方式更新属性C