1. WPFBind various data source Indexes
Datatable for binding various data sources to WPF
Object Data Sources bound to various data sources in WPF
XML data sources bound to various data sources in WPF
Element control attributes bound to various data sources in WPF
WPF binding Basics
Ii. binding element control attributes of various data sources to WPF
1. Bind the value path
<TextBlock Margin="327,378,331,363" Name="textBlock1" Text="{Binding ElementName=slider1, Path=Value}" /> <Slider Height="22" Minimum="0" Maximum="100" HorizontalAlignment="Left" Margin="135,346,0,0" Name="slider1" VerticalAlignment="Top" Width="100" />
The equivalent C # code is as follows:
textBlock1.SetBinding(TextBlock.TextProperty, new Binding() { Path = new PropertyPath("Value"), Source = slider1});
:
2. bind to the text. Length path
XAML:
<TextBox Height="23" HorizontalAlignment="Left" Margin="37,48,0,0" Name="textBox1" Text="{Binding Path=Text.Length, ElementName=textBox2, Mode=OneWay}" VerticalAlignment="Top" Width="120" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="37,92,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
The equivalent C # code is as follows:
textBox1.SetBinding(TextBox.TextProperty, new Binding() { Path =new PropertyPath("Text.Length"),Source = textBox2, Mode=BindingMode.OneWay});
3. bind to the Indexer
<TextBox Height="23" HorizontalAlignment="Left" Margin="37,48,0,0" Name="textBox1" Text="{Binding Path=Text.[2], ElementName=textBox2, Mode=OneWay}" VerticalAlignment="Top" Width="120" /><TextBox Height="23" HorizontalAlignment="Left" Text="Work Hard" Margin="37,92,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
Here the equivalent C # code is omitted
4. If the binding source is a set and the default element is used as the path, the syntax is as follows:
List<string> strCityList = new List<string>() { "Hangzhou", "Shanghai", "Beijing" }; textBox3.SetBinding(TextBox.TextProperty, new Binding("/") { Source = strCityList }); textBox4.SetBinding(TextBox.TextProperty, new Binding("/Length"){Source = strCityList, Mode = BindingMode.OneWay}); textBox5.SetBinding(TextBox.TextProperty, new Binding("/[3]") { Source = strCityList, Mode = BindingMode.OneWay });
As follows:
5. No path binding
This is a special case. The binding source itself is the data and does not need to be indicated by the path. The basic types such as string and INT are like this. They are the data, you cannot specify the attribute used to access this data. In this case, you only need to set path ".. See the following code:
<Window.Resources> <sys:String x:Key="myStr"> Work Hand Work Smart </sys:String> </Window.Resources>
XAML:
<TextBox Height="23" HorizontalAlignment="Right" Margin="0,65,199,0" Text="{Binding Path=. ,Source={StaticResource ResourceKey=myStr}, Mode=OneWay}" Name="textBox6" VerticalAlignment="Top" Width="141" />
The following code can be written as text = "{binding Path = ., source = {staticresource resourcekey = mystr}, mode = oneway} "or text =" {binding source = {staticresource resourcekey = mystr}, mode = oneway }"