数据绑定
1,绑定到控件:ElementName是数据源名,Path是绑定的内容
<StackPanel> <TextBlock Width="248" Height="24" Text="Colors:" TextWrapping="Wrap"/> <ListBox x:Name="lbColor" Width="248"> <ListBoxItem Content="Blue"/> <ListBoxItem Content="Green"/> <ListBoxItem Content="Yellow"/> <ListBoxItem Content="Red"/> <ListBoxItem Content="Purple"/> <ListBoxItem Content="Orange"/> </ListBox> <TextBlock Width="248" Height="24" Text="You selected color:" /> <TextBlock Width="248" Height="24" Text="{Binding ElementName=lbColor, Path=SelectedItem.Content}" Background="{Binding ElementName=lbColor,Path=SelectedItem.Content}"/> <TextBox Width="248" Height="24" Text="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=TwoWay}" x:Name="txtSelectedColor" Background="{Binding ElementName=lbColor, Path=SelectedItem.Content, Mode=OneWay}"/> </StackPanel>
2.绑定到资源的语法与绑定到控件的语法略有不同。绑定到控件时,可以设置绑定的 ElementName 和 Path 属性。但是绑定到资源时,需要设置 Source 属性,由于我们是绑定到 XmlDataProvider,所以还要设置绑定的 XPath 属性。
color.xml
<?xml version="1.0" encoding="utf-8" ?> <colors > <color name="red" primary="true"/> <color name="orange" primary="false"/> <color name="blue" primary="true"/> <color name="purple" primary="false"/> <color name="yellow" primary="true"/> <color name="green" primary="false"/> </colors>
Path为什么变为SelectedValue,因为ListBox的数据是绑定于{StaticResource Colors}
<StackPanel> <StackPanel.Resources> <XmlDataProvider x:Key="Colors" Source="color.xml" XPath="/colors"/> </StackPanel.Resources> <TextBlock Width="248" Height="24" Text="Colors:" TextWrapping="Wrap"/> <ListBox x:Name="lbColor" Width="248" Height="100" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource Colors}, XPath=color/@name}"> </ListBox> <TextBlock Width="248" Height="24" Text="You selected color:" /> <TextBlock Width="248" Height="24" Text="{Binding ElementName=lbColor, Path=SelectedValue}" Background="{Binding ElementName=lbColor,Path=SelectedValue}"/> <TextBox Width="248" Height="24" Text="{Binding ElementName=lbColor, Path=SelectedValue, Mode=TwoWay}" x:Name="txtSelectedColor" Background="{Binding ElementName=lbColor, Path=SelectedValue, Mode=OneWay}"/> <Button Content="focus"/> </StackPanel>
3,DataTemplate
Person.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace BindingTest { public class Person { private int _id; private string _fullName; private string _title; private string _city; public Person(int id, string fullName, string title, string city) { _id = id; _fullName = fullName; _title = title; _city = city; } public string City { get { return _city; } set { _city = value; } } public string Title { get { return _title; } set { _title = value; } } public string FullName { get { return _fullName; } set { _fullName = value; } } public int Id { get { return _id; } set { _id = value; } } } }
PersonService.cs
<Window x:Class="BindingTest.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" xmlns:svc="clr-namespace:BindingTest" > <StackPanel> <StackPanel.Resources> <ObjectDataProvider x:Key="persons" ObjectType="{x:Type svc:PersonService}" MethodName="GetPersonList" ></ObjectDataProvider> <DataTemplate x:Key="personLayout" DataType="Person"> <StackPanel Orientation="Vertical"> <TextBlock Text="{Binding Path=FullName}" FontWeight="Bold" Foreground="Blue"> </TextBlock> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=Title}"></TextBlock> <TextBlock Text=", "></TextBlock> <TextBlock Text="{Binding Path=City}"></TextBlock> </StackPanel> </StackPanel> </DataTemplate> </StackPanel.Resources> <TextBlock></TextBlock> <ListBox x:Name="lbPersons" ItemsSource="{Binding Source={StaticResource persons}}" ItemTemplate="{DynamicResource personLayout}" IsSynchronizedWithCurrentItem="True"/> </StackPanel> </Window>
x:key="persons"和{x:Type svc:PersonService}定义名为persons的类PersonService对象
MethodName属性调用类PersonService的对象方法GetPersonList
listBox的IsSynchronizedWithCurrentItem 属性设置为 true 以确保选定的项始终对应于 ItemCollection 中的 CurrentItem 属性。例如,假定有两个 ListBox 控件,这两个控件的 ItemsSource 属性设置为相同源。对这两个列表框将 IsSynchronizedWithCurrentItem 设置为 true 以确保在每个 ListBox 中的选定项相同。