2017-07-26 65 views
0

Binding類提供監聽財產路徑使用「綁定」級聽屬性路徑更改爲代碼隱藏操作

例子:

<TextBlock Text="{Binding A.B.C.D.E}" /> 

反映了產業鏈

我的任何變化想在後面的代碼中使用這種機制。

我想要做什麼:

var binding = new Binding{ 
    Source = ViewModel, 
    Path = new PropertyPath("A.B.C.D.E") 
}; 
// not available 
binding.ResultChanged += OnBindingResultChanged; 

但「綁定」不提供任何事件

+0

綁定需要依賴項屬性作爲目標。綁定本身並沒有做任何事情。它是一個描述符,當您使用SetBinding()方法設置它時,綁定引擎使用該描述符來知道要監視哪些屬性和事件以更新值。如果你想利用它,你需要創建一個'DependencyObject'和一個'DependencyProperty',你用它作爲綁定的目標。 –

回答

0

我覺得你應該使用Onpropertychanged事件。 你實現它這樣

public class YourClass : window, INotifyPropertyChanged 

    public string A.B.C.D.E {Get ; set ;} 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(string name) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(name)); 
     } 
    } 
在此之後

在你的XAML當屬性更改您可以引發事件。

<TextBlock Text="{Binding A.B.C.D.E}" textchanged="YOURMETHODENAME"/> 

當調用此方法時,可以更改後面代碼中屬性的值。

public void YOURMETHODENAME (sender e, routeeventargs) 
{ 
    //dostuff with your property 
    OnPropertyChanged ("A.B.C.D.E) 
} 

這樣你就可以改變代碼背後的屬性和用戶輸入的值。

+0

A.B.C.D.E是一個鏈式表達 像CurrentUser.Address.City –