标签:wpf data binding
Binding 的源也就是数据的源头。Binding对源的要求并不苛刻------只要它是一个对象,并且通过属性(Property)公开自己的数据,它就能作为Binding 的源。
前面一个例子已经向大家证明,如果想让作为Binding源的对象具有自动通知Binding自己属性值已经已经变化的能力,那么就需要让类实现INotifyChanged接口并在属性的Set语句中激发PropertyChanged事件。在日常生活中,除了使用这种对象作为数据源之外,我们还有更多的选择,比如用一个控件做为另一个控件的数据源,把集合作为ItemControl的数据源、使用XML作为TreeView或Menu的数据源等。
<Window x:Class="WpfApplication6.wnd631" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="wnd631" Height="110" Width="300"> <StackPanel> <TextBox x:Name="_txtBox" BorderBrush="Black" Margin="5"/> <Slider x:Name="_slider" Margin="5" Maximum="100"/> </StackPanel> </Window>
_txtBox.SetBinding(TextBox.TextProperty, new Binding("Value") { Source = _slider, Mode = BindingMode.OneWay, // 更新的方向 UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged // 更新的时机 });
// 使用文本的长度作为Path _txtBox2.SetBinding(TextBox.TextProperty, new Binding("Text.Length") { Source = _txtBox1, Mode = BindingMode.OneWay });
// 集合元素的默认元素当作Path List<string> strList = new List<string>() { "Tim", "lkjh" }; _txtBox3.SetBinding(TextBox.TextProperty, new Binding("/") { Source = strList, Mode = BindingMode.OneWay }); _txtBox4.SetBinding(TextBox.TextProperty, new Binding("/Length") { Source = strList, Mode = BindingMode.OneWay }); _txtBox5.SetBinding(TextBox.TextProperty, new Binding("/[2]") { Source = strList, Mode = BindingMode.OneWay });
// 没有Path的Source string myStr = "hfdahfdlh;ahdfhfsahdfhasdfa"; _txtBox6.SetBinding(TextBox.TextProperty, new Binding(".") { Source = myStr, Mode = BindingMode.OneWay });
<Window x:Class="WpfApplication6.wnd633" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="wnd633" Height="400" Width="600"> <Grid> <Label Content="多级路径(一路点下去)" Margin="10,10,358,328"/> <TextBox x:Name="_txtBox1" Margin="10,47,358,291" /> <TextBox x:Name="_txtBox2" Margin="10,121,358,217" /> <Label Content="集合的默认元素(一路斜下去)" Margin="333,10,35,328"/> <TextBox x:Name="_txtBox3" Margin="333,47,35,291" /> <TextBox x:Name="_txtBox4" Margin="333,84,35,254" /> <TextBox x:Name="_txtBox5" Margin="333,121,35,217" /> <Label Content="没有Path的Source" Margin="10,188,358,150"/> <TextBox x:Name="_txtBox6" Margin="10,225,358,113" /> </Grid> </Window>
// 使用文本的长度作为Path _txtBox2.SetBinding(TextBox.TextProperty, new Binding("Text.Length") { Source = _txtBox1, Mode = BindingMode.OneWay }); // 集合元素的默认元素当作Path List<string> strList = new List<string>() { "Tim", "lkjh" }; _txtBox3.SetBinding(TextBox.TextProperty, new Binding("/") { Source = strList, Mode = BindingMode.OneWay }); _txtBox4.SetBinding(TextBox.TextProperty, new Binding("/Length") { Source = strList, Mode = BindingMode.OneWay }); _txtBox5.SetBinding(TextBox.TextProperty, new Binding("/[2]") { Source = strList, Mode = BindingMode.OneWay }); // 没有Path的Source string myStr = "hfdahfdlh;ahdfhfsahdfhasdfa"; _txtBox6.SetBinding(TextBox.TextProperty, new Binding(".") { Source = myStr, Mode = BindingMode.OneWay });
WPF Data Binding之控件作为源(Source)与路径(Path)【一】
标签:wpf data binding
原文地址:http://blog.csdn.net/aoshilang2249/article/details/44850247