WPF TextBox绑定

时间:2012-10-15 08:02:33

标签: c# wpf textbox bind

我有一个名为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可以立即显示它,但是失败了,我该怎么办? / p>

1 个答案:

答案 0 :(得分:0)

如果您希望视图中的更改除外,您应该通知视图有关更改。

因此,只需在INotifyPropertyChanged类中实现VoucherEntity接口,并在设置客户支持后触发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");
            }
        }
    }
}