WPF中提供的代碼幫頂技術有很多種,可以根據不同的情況使用不同的方法,十分靈活,下面根據不同的情況進行設置。
1、通過代碼綁定
1 <TabItem Header="CodeBinding"> 2 <Grid> 3 <Button Content="Button" HorizontalAlignment="Left" Margin="49,116,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/> 4 <TextBox Name="tbCode" HorizontalAlignment="Left" Height="22" Margin="49,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 5 <Button Content="Button" HorizontalAlignment="Left" Margin="259,116,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/> 6 <TextBox Name="tbCode1" HorizontalAlignment="Left" Height="22" Margin="259,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> 7 </Grid> 8 </TabItem>
你可以這樣在代碼設置
先看看studen類的實現
1 class Student : INotifyPropertyChanged 2 { 3 public string Age { get; set; } 4 public string Id { get; set; } 5 private string m_Name; 6 public string Name 7 { 8 get 9 { 10 return m_Name; 11 } 12 set 13 { 14 m_Name = value; 15 if (PropertyChanged != null) 16 { 17 this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name")); 18 } 19 } 20 } 21 22 public event PropertyChangedEventHandler PropertyChanged; 23 }
這個各類繼承自INotifyPropertyChanged,這個接口的事要目的就是為了在Name屬性被改寫的時候會通知到你綁定的目標對象去,使目標也已一起變化。屬性如果要綁定,需要繼承INotifyPropertyChanged接口。
1 // 摘要: 2 // 向客戶端發出某一屬性值已更改的通知。 3 public interface INotifyPropertyChanged 4 { 5 // 摘要: 6 // 在更改屬性值時發生。 7 event PropertyChangedEventHandler PropertyChanged; 8 }
其實我一開始很糾結,因為PropertyChanged沒有讓我親自實現,總是覺得很別扭,這廝是怎么被工作的,其實當你完成數據綁定的時候,會自動給這個時間加一個實現的。我們不需要去管它。
點擊按鈕的話我會創建一個student類,給Name屬性賦值,然后將這個對象綁定到tbcode的Text屬性上。
1 private void Button_Click_1(object sender, RoutedEventArgs e) 2 { 3 Student st = new Student(); 4 st.Name = "Jim"; 5 Binding bd = new Binding(); 6 bd.Source = st; 7 bd.Path = new PropertyPath("Name"); 8 BindingOperations.SetBinding(tbCode, TextBox.TextProperty, bd); 9 }
里面有兩個比較重要的屬性,一個是source一個是path,source是Binding的的源,就是說你的數據源在哪里,另外一個是path就是路徑,就是要綁定的具體的數據源是哪個,設置好這兩個以后,就可以把這個Binding綁定到你的目標對象上——tbCode,使用BindingOperations.SetBinding,第一個參數是要綁定的對象,第二個參數是你要在這個對象的那個屬性上,第三個參數就是之前設置的Binding。
當然這種寫法也可以簡化
1 private void Button_Click_2(object sender, RoutedEventArgs e) 2 { 3 Student st = new Student(); 4 st.Name = "Mike"; 5 tbCode1.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = st }); 6 }
tbCode1.SetBinding這個的實現就是把上面的BindingOperations.SetBinding進行了一個封裝。
不過有一點我不明白,我是先給st賦值的,然后再進行綁定,為什么這個時候也會同時textblock空間進行更新呢?希望高手指點。
2、UIElementBInding綁定
這個綁定很簡單,也很常用。
如果我有一個silder,一個textbox來顯示他的value,應該怎么做?當然你可以實現ValueChanged方法。但是用綁定會更加的靈活。
1 <TabItem Header="UIElementBInding"> 2 <Grid> 3 <Slider x:Name="slider" HorizontalAlignment="Left" Margin="50,195,0,0" VerticalAlignment="Top" Width="393"/> 4 <TextBox HorizontalAlignment="Left" Height="23" Margin="113,77,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Value, ElementName=slider}"/> 5 6 </Grid> 7 </TabItem>
你不需要寫一行C#代碼就能夠完成。Text="{Binding Value, ElementName=slider}"意思是要綁定到哪個控件(slider)的哪個屬性(Value)上。
C#的綁定有很多,這里先介紹兩種,剩下的后面會一一介紹。
代碼地址:
https://github.com/Dothegod/WPF_Training_Sample