參考官方:https://docs.microsoft.com/zh-cn/dotnet/framework/wpf/data/data-binding-wpf
實例程序:https://files.cnblogs.com/files/sntetwt/WPFBinding.zip
1、指定綁定源
WPF雙向數據同步:目標屬性(UI)和源屬性(CS)數據同步。
實現雙向數據同步數據源需要實現依賴屬性INotifyPropertyChanged接口,因為依賴屬性有垂直的內嵌變更通知機制。
INotifyPropertyChanged是用於實現界面通知。
DependencyObject是實現依賴對象的依賴屬性。
名空間:
using System.ComponentModel;
實現依賴屬性INotifyPropertyChanged接口
using System;
using System.ComponentModel;
namespace WPFBinding
{
/// <summary>
/// 實現INotifyPropertyChanged接口
/// </summary>
public class Users : INotifyPropertyChanged
{
/// <summary>
/// 姓名
/// </summary>
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
/// <summary>
/// Email
/// </summary>
private string _Email;
public string Email
{
get
{
return _Email;
}
set
{
_Email = value;
OnPropertyChanged("Email");
}
}
protected internal virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
2、MVVM架構,ViewModel初始化
namespace WPFBinding
{
public class ViewModel
{
public Users user { get; set; }
public ViewModel()
{
this.user = new Users();
}
}
}
3.1、交互邏輯,實例化數據(方式一)
using System;
using System.Windows;
namespace WPFBinding
{
/// <summary>
/// MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public ViewModel viewModel;
public MainWindow()
{
InitializeComponent();
this.viewModel = new ViewModel();
this.Loaded += (s, e) =>
{
this.DataContext = viewModel;
this.viewModel.user = new Users()
{
Name = "楊秀徐",
Email = "471812366@qq.com"
};
};
}
}
}
3.2、XAML綁定數據(方式一)
<TextBox Name="txtName" Text="{Binding user.Name}"></TextBox>
<TextBox Name="txtEmail" Text="{Binding user.Email}"></TextBox>
4.1、交互邏輯,實例化數據(方式二)
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPFBinding
{
/// <summary>
/// GetBinding.xaml
/// </summary>
public partial class GetBinding : Window
{
public ViewModel viewModel;
public GetBinding()
{
InitializeComponent();
this.viewModel = new ViewModel();
this.Loaded += (s, e) =>
{
this.DataContext = viewModel;
this.viewModel.user = new Users()
{
Name = "楊秀徐",
Email = "471812366@qq.com"
};
//綁定依賴屬性Name
txtName.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = viewModel.user });
//綁定元素屬性Text
lblName.SetBinding(ContentProperty, new Binding("Text") { Source = txtName });
//綁定依賴屬性Email
txtEmail.SetBinding(TextBox.TextProperty, new Binding("Email") { Source = viewModel.user });
//綁定元素屬性Text
lblEmail.SetBinding(ContentProperty, new Binding("Text") { Source = txtEmail });
};
}
}
}
4.2、XAML元素(方式二)
<Label Name="lblName"></Label> <TextBox Name="txtName"></TextBox> <Label Name="lblEmail"></Label> <TextBox Name="txtEmail"></TextBox>

5.1、Binding對象的屬性說明
屬性名 描述
1、Converter:轉換器,將綁定的內容轉換成自己需要的內容。自定義轉換類 必須繼承於:IValueConverter接口
2、ElementName:綁定的源對象,本人理解 專程用於UI控件之間屬性的綁定
3、FallbackValue :綁定無法返回有效值時的默認顯示值
4、Mode:綁定方式,枚舉類型 Default OneWay TwoWay OneTime OneWayToSource
5、Path:屬性 路徑,用來指定要綁定數據源的路徑,其性質是一個屬性,該屬性該當是依靠屬性,也即便能夠告終積極更新機制的【單個類實現INotifyPropertyChanged、集合要 實現INotifyCollectionChanged接口】
6、RelativeSource:常用於自身綁定或者數據模板中來指定綁定的源對象及控件模塊中的綁定。
7、Source:源對象,控件或自定義對象等。
8、StringFormat:格式化表達式
9、UpdateSourceTrigger:在雙向綁定時TwoWay 或 OneWayToSource 時。用來確定屬性更改的時機。UpdateSourceTrigger枚舉類型:Default,PropertyChanged,LostFocus,Explicit。
10、ValidationRules:驗證規則.可以被設置為一個或多個派生自ValidationRule的對象,每個規則都會檢查特定的條件並更具結果來標記數據的有效性
5.2、Mode指定綁定的方向
數據綁定模式共有四種:OneTime、OneWay、OneWayToSource和TwoWay,默認是TwoWay。
TwoWay 當發生更改時的目標屬性或源屬性更新目標屬性。
OneWay 僅當源屬性更改時,請更新目標屬性。
OneTime 僅當應用程序啟動時或時,請更新目標屬性DataContext發生了更改。
OneWayToSource 目標屬性更改時,請更新源屬性。
Default 默認值將導致Mode要使用的目標屬性的值。
5.3、UpdateSourceTrigger 四種用來確定屬性更改的時機,對於 Model=TwoWay 及 OneWayToSource是源數據改變時機。
6、UI屬性綁定數據對象。
6.1 、UI屬性直接綁定實例對象 。實例:Text="{Binding Path=EntryDate, StringFormat=yyyy-MM-dd}"
6.2 、UI屬性直接綁定靜態對象。實例:DataContext="{x:Static local:GlobalData.user}"
6.3 、UI屬性綁定資源中的對象。DataContext="{StaticResource ResourceKey=userKey}"
7、清除綁定
BindingOperations.ClearBinding(txtBlock, TextBlock.TextProperty);
8、集合雙向綁定
WPF 提供 ObservableCollection<T> 類,它是實現 INotifyCollectionChanged 接口的數據集合的內置實現。
public class Users : ObservableCollection<Users>
{
public Users() : base()
{
}
}
