绑定到控件时,可以设置绑定的 ElementName 和 Path 属性。但是绑定到资源时,需要设置 Source 属性,由于我们是绑定到 XmlDataProvider,所以还要设置绑定的 XPath 属性,XML数据绑定就是指控件的数据源来自一个XML文件,项目中有一个Colors.xml文件,在项目中右键添加->新建项->选择xml文件,输入内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<colors >
<color name="Pink"/>
<color name="Red"/>
<color name="Purple"/>
<color name="Cyan"/>
<color name="Gray"/>
<color name="Black"/>
</colors>
接着在grid的第2行第1列添加一个堆叠控件StackPanel控件,具体代码如下(接上一节代码):
<Window x:Class="WpfApp6.BindWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp6"
mc:Ignorable="d"
Title="BindWindow" Height="765" Width="980">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="250"/>
<RowDefinition Height="200"/>
<RowDefinition Height="138*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.Column="0">
<TextBlock Width="248" Height="24" Text="简单绑定,股票名称:" TextWrapping="Wrap" FontSize="15"/>
<ListBox Name="mylista" Width="348" Height="150" FontSize="15" Margin="0,0">
<ListBoxItem Content="全通教育"/>
<ListBoxItem Content="京大智慧"/>
<ListBoxItem Content="宝钢股份"/>
<ListBoxItem Content="浦发银行"/>
<ListBoxItem Content="工商银行"/>
<ListBoxItem Content="中国建筑"/>
<ListBoxItem Content="长城汽车"/>
<ListBoxItem Content="山东电子"/>
<ListBoxItem Content="浙江吉利"/>
</ListBox>
<TextBlock Width="248" Height="24" Text="你选中的股票:" Margin="0,5,110,0" FontSize="15"/>
<!--ElementName 属性指示要绑定的控件名称。Path 属性指示绑定到ListBox元素的属性-->
<TextBlock Width="248" Height="24" Text="{Binding ElementName=mylista,Path=SelectedItem.Content}" FontSize="15" Margin="0,0,110,0"/>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Height="240" Margin="10,10,0,0" VerticalAlignment="Top" Width="466">
<Label Content="绑定多个控件及单向双向绑定和更新触发器" FontSize="15" />
<ListBox Height="73" Margin="40,0,60,0" FontSize="15" Name="mylistb">
<ListBoxItem Content="green"/>
<ListBoxItem Content="blue"/>
<ListBoxItem Content="black"/>
<ListBoxItem Content="yellow"/>
<ListBoxItem Content="orange"/>
</ListBox>
<Label Content="背景色" Height="23" FontSize="12" Margin="0,2,366,0"/>
<!--TwoWay双向绑定,OneWay单向绑定-->
<!--UpdateSourceTrigger意指更新触发器,表示更新数据源方式:Explicit、LostFocus(默认) 和 PropertyChanged
1:Explicit,则不会更新源,除非从代码中调用 BindingExpression.UpdateSource
2:LostFocus ,默认值,指示该控件失去焦点时才会更新数据源。
3:PropertyChanged,控制属性每次发生更改时就立即更新数据源。-->
<TextBox FontSize="15" Text="{Binding ElementName=mylistb, Path=SelectedItem.Content, Mode=TwoWay,UpdateSourceTrigger=LostFocus}" Background="{Binding ElementName=mylistb, Path=SelectedItem.Content, Mode=OneWay}" Margin="0,0,306,0"/>
<TextBox FontSize="15" Text="{Binding ElementName=mylistb, Path=SelectedItem.Content, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="0,10,306,0"/>
<TextBlock TextWrapping="Wrap" Text="" Height="39" Margin="0,10,0,0" Background="{Binding ElementName=mylistb, Path=SelectedItem.Content, Mode=OneWay}"/>
</StackPanel>
<!--XML数据绑定-->
<StackPanel HorizontalAlignment="Left" Height="180" Margin="10,10,0,0" Grid.Row="1" Grid.Column="0" VerticalAlignment="Top" Width="476">
<!--Resources属性指定外部资源为XmlDataProvider-->
<StackPanel.Resources>
<!--Source 属性指定资源文件名,XPath 属性指定路径名-->
<XmlDataProvider x:Key="MyColors" Source="Colors.xml" XPath="colors"/>
</StackPanel.Resources>
<Label Content="XML数据绑定"/>
<ListBox Height="100" Margin="0,0,10,0" Name="mylistc" ItemsSource="{Binding Source={StaticResource MyColors},XPath=color/@name}"/>
<Label Content="选中的颜色"/>
<TextBlock TextWrapping="Wrap" Background="YellowGreen" Text="{Binding ElementName=mylistc, Path=SelectedValue, Mode=OneWay}"/>
</StackPanel>
</Grid>
</Window>
效果如下:
效果扛扛的,继续干。