0

我有一个名为 VoucherEntity 的类,包括一个名为“Customer”的属性,一个 CustomerEntity 类的对象,所以我有以下代码,

<TextBox Height="23" IsReadOnly="False" HorizontalAlignment="Stretch" Margin="124,48,174,0" Name="txt_customer" VerticalAlignment="Top" Text="{Binding Path=Customer.Name}" />

在 .cs 文件中,我有以下代码

_voucher = new VoucherEntity();
this.DataContext = _voucher;

意思是,一开始Customer属性为null,点击按钮后,我会给_voucher的Customer属性一个CustomerEntity对象,然后希望TextBox能立即显示,但是失败了,怎么办?

4

1 回答 1

0

如果您想排除视图中的更改,您应该通知视图有关更改。

因此,只需INotifyPropertyChanged在类中实现接口VoucherEntity并在设置 Customer 道具后触发PropertyChanged事件

public class VoucherEntity: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void FirePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private CustomerEntity _customer;
    public CustomerEntity Customer
    {
        get {return _customer;}
        set
        {
            if (_customer != value)
            {
                _customer= value;
                FirePropertyChanged("Customer");
            }
        }
    }
}
于 2012-10-15T08:07:05.063 回答