WPF学习历程2----Binding(一)
Binding基础
Eg1让数据源的类实现System.ComponentModel名称空间中的INotifyPropertyChanged接口,当Binding设置了数据源后,Binding就会自动侦听来自这个接口的PropertyChanged事件
xaml代码:
<StackPanel>
<TextBox x:Name="txt_Name" BorderBrush="Blue" Margin="5"></TextBox>
<Button Content="增加" Margin="5" Click="Button_Click"></Button>
</StackPanel>
C#代码:
public partial class MainWindow : Window
{
Student stu;
public MainWindow()
{
InitializeComponent();
stu = new Student();
Binding binding= new Binding();
binding.Source = stu;
binding.Path = new PropertyPath("Name");
BindingOperations.SetBinding(this.txt_Name, TextBox.TextProperty, binding);
}
public void Button_Click(object sender, RoutedEventArgs e)
{
stu.Name += "Name";
}
}
class Student : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string name;
public string Name
{
get { return name; }
set
{
name = value;
if (this.PropertyChanged != null)
{
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
}
其中代码可以简写,TextBox这类UI元素的基类FrameworkElement对BindingOperations.SetBinding(……)方法进行了封装,封装的结果也叫SetBinding
C#代码:
public partial class MainWindow : Window
{
Student stu;
public MainWindow()
{
InitializeComponent();
this.txt_Name.SetBinding(TextBox.TextProperty, new Binding("Name") { Source = stu = new Student() });
}
public void Button_Click(object sender, RoutedEventArgs e)
{
stu.Name += "Name";
}
}
建立模型
Binding的源与路径
方法一:
<StackPanel>
<TextBox x:Name="txt_Name" Text="{ Binding Path=Value, ElementName=slider1}" BorderBrush="Blue" Margin="5"></TextBox>
<Slider x:Name="slider1" Maximum="100" Minimum="0" Margin="5"></Slider>
</StackPanel>
方法二:
C#代码
Binding binding = new Binding() { Path = new PropertyPath("Value"), Source = this.slider1 };
this.txt_Name.SetBinding(TextBox.TextProperty, binding);
或者
Binding binding = new Binding("Value") { Source=this.slider1};
this.txt_Name.SetBinding(TextBox.TextProperty, binding);
Binding多路径
<StackPanel>
<TextBox x:Name="textbox1" BorderBrush="Blue" Margin="5"></TextBox>
<TextBlock Text="文本的长度" Margin="5"></TextBlock><TextBox x:Name="showlength" Text="{Binding Path=Text.Length,ElementName=textbox1,Mode=OneWay}" BorderBrush="Blue" Margin="5"></TextBox>
<TextBlock Text="取得第3个文本"></TextBlock><TextBox x:Name="showIndex" Text="{Binding Path=Text[2],ElementName=textbox1,Mode=OneWay}" BorderBrush="Blue" Margin="5"></TextBox>
</StackPanel>
或者
this.showlength.SetBinding(TextBox.TextProperty, new Binding("Text.Length") { Source = this.textbox1, Mode = BindingMode.OneWay });
this.showIndex.SetBinding(TextBox.TextProperty, new Binding("Text[2]") { Source = this.textbox1, Mode = BindingMode.OneWay });
当使用一个集合或者Dadaview作为Binding源时
List<string> stringList = new List<string>() { "iPhone", "Lumia900", "HTC" };
this.textbox1.SetBinding(TextBox.TextProperty, new Binding("/") { Source = stringList });
this.textbox2.SetBinding(TextBox.TextProperty, new Binding("/Length") { Source = stringList, Mode = BindingMode.OneWay });
this.textbox3.SetBinding(TextBox.TextProperty, new Binding("/[2]") { Source = stringList, Mode = BindingMode.OneWay });
“没有Path”的Binding
<StackPanel>
<StackPanel.Resources>
<sys:String x:Key="myString">
iPhone Lumia
HTC
</sys:String>
</StackPanel.Resources>
<TextBlock x:Name="textblock1" TextWrapping="Wrap" Text="{Binding Path=.,Source={StaticResource ResourceKey=myString}}" FontSize="16" Margin="5"></TextBlock>
</StackPanel>
或者
string myString="iphone,Lumia900,HTC";
this.textblock1.SetBinding(TextBlock.TextProperty, new Binding(".") { Source = myString });
本是菜鸟,偶做老鸟,略读半卷书,坐井说天阔。大志无所为,海斗量得失,年到老时方恨晚,怒指生不逢时。