2014-09-11 122 views
-1

這是我的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"); } 
}  

當我設置值這兩個propertie即。以標識和名稱

,但它不是對通知名稱...

+0

你忘了設置this.DataContext?這真的不是一個模型,它只是一個屬性:) – 2014-09-11 05:35:54

+0

哦是啊!!其財產.. – user3502966 2014-09-11 05:47:52

+0

我也設置datacontext,仍然不工作:( – user3502966 2014-09-11 05:48:34

回答

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> 
+0

我解決了它..我剛剛使用另一個集合,它的工作.. – user3502966 2014-09-11 06:20:31

+0

令人震驚不知道爲什麼發生這種情況..previous收集聲明correctoly – user3502966 2014-09-11 06:21:47

+0

真棒,尋找錯誤可能會一個有價值的冒險。祝你好運:) – 2014-09-11 06:50:26