元素綁定
數(shù)據(jù)綁定最簡單的形式是,源對象是WPF元素而且源屬性是依賴屬性。依賴項屬性具有內(nèi)置的更改通知支持,當在源對象中改變依賴項屬性的值時,會立即更新目標對相中的綁定屬性。
<!--Xaml程序 -->
<Grid>
<StackPanel>
<Button x:Name="btn" Margin="20" Click="btn_Click" Width="40" Height="35"/>
<TextBox Text="{BindingElementName=txt2,Path=Text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<!-- TextBox綁定屬性名為txt2,綁定其Text,雙向綁定,屬性改變時更新 -->
<TextBlock x:Name="txt2" />
</StackPanel>
</Grid>
//后臺程序
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btn_Click(object sender, RoutedEventArgs e)
{
txt2.Text += "k";
}
}
BindingMode枚舉值
| 名稱 | 說明 |
|---|---|
| OneWay | 當源屬性變化時更新目標屬性 |
| TwoWay | 當源屬性變化時更新目標屬性,當目標屬性變化時更新源屬性 |
| OneTime | 最初根據(jù)源屬性設置目標屬性,其后的改變會忽略。 |
| OneWayToSource | 與OneWay類型相似,但方向相反。 |
| Default | 此類綁定依賴于目標屬性 |
UpdateSourceTrigger
| 名稱 | 說明 |
|---|---|
| Default | 默認值,與依賴屬性有關 |
| Explicit | 必須在顯示地調(diào)用BindingExpression.UpdateSource的情況下才更新源。 |
| LostFocus | 控件失去焦點的時候更新源值 |
| PropertyChanged | 綁定的目標值改變時更新。 |
數(shù)據(jù)綁定
<!-- Xaml程序 -->
<Grid>
<StackPanel>
<DataGrid ItemsSource="{Binding PerList}" IsReadOnly="True"></DataGrid>
<ComboBox x:Name="com" ItemsSource="{Binding PerList}" DisplayMemberPath="Name" Margin="10" Width="80" Height="30"/>
<!--綁定PerList,展示其Name屬性 -->
</StackPanel>
</Grid>
//后臺程序
public partial class MainWindow : Window
{
public List<string> LT { get; set; }
List<Person> Perss { get; set; }
public MainWindow()
{
PersonList perlist = new PersonList(); //創(chuàng)建一個PersonList對象
Perss = new List<Person>() { new Person("Getsu1","男"), new Person("Getsu2", "男"), new Person("Getsu3", "男")};
perlist.PerList = Perss; //給PerList屬性賦值
this.DataContext = perlist; //將perlist加入上下文,設置之后綁定才會有效
}
}
public class PersonList
{
public List<Person> PerList { get; set; }
public PersonList()
{
PerList = new List<Person>();
}
public class Person
{
private string _name;
private string _sex;
public string Name
{
get { return _name; }
set{ _name = value; }
}
public string Sex
{
get { return _sex; }
set{ _sex = value; }
}
public Person(string name, string sex)
{
Name = name;
Sex = sex;
}
}
}
INotifyPropertyChanged
屬性更改通知接口。向客戶端發(fā)出某一屬性已更改的通知。
event PropertyChangedEventHandler PropertyChanged;
PropertyChanged 在屬性改變時發(fā)生。
<!-- X -->
<StackPanel>
<TextBox Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBox Text="{Binding Sex,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<!-- 不會隨著TextBox內(nèi)容的改變而改變-->
<TextBlock Text="{Binding Sex,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<!-- 會隨著TextBox內(nèi)容的改變而改變-->
</StackPanel>
//后臺程序
public partial class MainWindow : Window
{
public MainWindow()
{
Person per = new Person("kakarot", "Male");
this.DataContext = per;
InitializeComponent();
}
}
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _name;
private string _sex;
public string Name
{
get { return _name; }
set
{
_name = value;
//NotifyPropertyChanged("Name");
}
}
public string Sex
{
get { return _sex; }
set
{
_sex = value;
NotifyPropertyChanged("Sex");
}
}
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public Person(string name, string sex)
{
Name = name;
Sex = sex;
}
}
特性
public string Sex
{
get { return _sex; }
set
{
_sex = value;
NotifyPropertyChanged();
//使用特性之后可以自動獲取屬性名
}
}
//特性的表示
private void NotifyPropertyChanged([CallerMemberName]String info="默認值")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}