Notify Property Changed不起作用



这是我的xml代码

<TextBlock Grid.Column="0" Tag="{Binding id,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

这是我的型号

public string _Name;
public string Name
{
    get { return _Name; }
    set { _Name = value; RaisePropertyChanged("Name"); }
}      

当我为这两个属性设置值时,即id和Name

但它没有通知Name。。。

带更新的简单数据绑定示例。您可以将此作为开始的参考:)

public partial class MainWindow : Window, INotifyPropertyChanged
{
    // implement the INotify
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    private string _mytext;
    public String MyText
    {
        get { return _mytext;  }
        set { _mytext = value; NotifyPropertyChanged("MyText"); }
    }
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = this;             // set the datacontext to itself :)
        MyText = "Change Me";
    }
}

<TextBlock Text="{Binding MyText}" Foreground="White" Background="Black"></TextBlock>

最新更新