通知属性已更改无效

时间:2014-09-11 05:28:56

标签: c# xaml inotifypropertychanged

这是我的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和名称

但它没有通知姓名......

1 个答案:

答案 0 :(得分:0)

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

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>