微软参考:
数据绑定概述:https://msdn.microsoft.com/zh-cn/library/ms752347(v=vs.110).aspx
数据绑定概述(XAML): https://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/hh758320.aspx
绑定源概述: https://msdn.microsoft.com/zh-cn/library/ms743643(v=vs.110).aspx
绑定声明概述: https://msdn.microsoft.com/zh-cn/library/ms752300(v=vs.110).aspx
如何:指定绑定源: https://msdn.microsoft.com/zh-cn/library/ms746695(v=vs.110).aspx
FrameworkElement.DataContext 属性:
https://msdn.microsoft.com/zh-cn/library/system.windows.frameworkelement.datacontext(v=vs.110).aspx
示例:
界面:
<Window x:Class="BindingData.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="138*" />
<RowDefinition Height="173*" />
</Grid.RowDefinitions>
<StackPanel Grid.RowSpan="2">
<TextBox x:Name="textBox1" Margin="5" Text="{Binding Path=Age}"/>
<Button Content="Pass A Year" x:Name="button1" Click="button1_Click" Margin="5" Height="25" Width="190" />
</StackPanel>
<StackPanel Grid.Row="1">
<ListBox x:Name="listboxColor" Height="55" Width="461">
<ListBoxItem>Yellow</ListBoxItem>
<ListBoxItem>Blue</ListBoxItem>
<ListBoxItem>Red</ListBoxItem>
<ListBoxItem>Black</ListBoxItem>
</ListBox>
<TextBlock x:Name="textBlockShowColor" Height="35" Margin="0"
Text="{Binding ElementName = listboxColor,Path = SelectedItem.Content}"
Background="{Binding ElementName = listboxColor,Path = SelectedItem.Content}" Width="400" />
</StackPanel>
</Grid>
</Window>
CS代码:
using System.Windows;
using System.Windows.Data;
using System.Windows.Controls;
using System.ComponentModel;
namespace BindingData
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public Person per;
public MainWindow()
{
InitializeComponent();
//数据源
per = new Person();
//xaml中绑定 设置数据源
this.DataContext = per;
//cs代码中绑定,xaml中不需 Binding
#if true
//写法1
//Binding myBinding = new Binding();
//myBinding.Source = per;
//myBinding.Path = new PropertyPath("Age");
#elif false
//写法2
Binding myBinding = new Binding(){Path= new PropertyPath("Age"),Source = per);
#else
//写法3
Binding myBinding = new Binding("Age"){Source = per};
#endif
//使用bingding对象 绑定 绑定源 和绑定目标 ,将Textbox绑定到per上
//BindingOperations.SetBinding(this.textBox1, TextBox.TextProperty, myBinding);
//this.textBox1.SetBinding(TextBox.TextProperty,myBinding); //也可以这样建立绑定
////////////////////////////////////////////////////////////////////////////////////
Binding myBinding2 = new Binding(); //创建新的绑定对象
myBinding2.Source = this.listboxColor; //绑定源
myBinding2.Path = new PropertyPath("SelectedItem.Content"); //绑定源路径
//将绑定源与绑定目标绑定起来 这是是与绑定目标textBlockShowColor的TextProperty绑定
BindingOperations.SetBinding(this.textBlockShowColor, TextBlock.TextProperty, myBinding2);
Binding myBindingbcGround = new Binding();
myBindingbcGround.Source = this.listboxColor;
myBindingbcGround.Path = new PropertyPath("SelectedItem.Content");
BindingOperations.SetBinding(this.textBlockShowColor, TextBlock.BackgroundProperty, myBindingbcGround);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
per.Age++;
}
}
public class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int age;
public int Age
{
get
{
return age;
}
set
{
age = value;
if (this.PropertyChanged != null)//激发事件,参数为Age属性
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Age"));
}
}
}
}
}