首页 > 代码库 > WPF listbox数据绑定

WPF listbox数据绑定

<Window x:Class="DemoDataTempPlate.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">    <Window.Resources>        <DataTemplate x:Key="listboxDataTempPlateStyle">            <Grid Margin="20,5,20,0">                <Grid.RowDefinitions>                    <RowDefinition/>                    <RowDefinition/>                </Grid.RowDefinitions>                <Grid.ColumnDefinitions>                    <ColumnDefinition Width="64"/>                    <ColumnDefinition Width="180"/>                    <ColumnDefinition Width="64"/>                    <ColumnDefinition Width="180"/>                </Grid.ColumnDefinitions>                <TextBlock Grid.Row="0" Grid.Column="0" Text="姓名:"/>                <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding XPath=Name}"/>                <TextBlock Grid.Row="0" Grid.Column="2" Text="年龄:"/>                <TextBlock Grid.Row="0" Grid.Column="3" Text="{Binding XPath=Age}"/>                <TextBlock Grid.Row="1" Grid.Column="0" Text="性别:"/>                <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding XPath=Sex}"/>            </Grid>        </DataTemplate>                <!--<Style TargetType="ListBoxItem">            <Style.Triggers>                <Trigger Property="ListBoxItem.IsSelected" Value="http://www.mamicode.com/True">                    <Setter Property="Background" Value="http://www.mamicode.com/White"/>                </Trigger>            </Style.Triggers>        </Style>-->                <Style TargetType="ListBox">            <Setter Property="ItemContainerStyle">                <Setter.Value>                    <Style TargetType="ListBoxItem">                        <Setter Property="Template">                            <Setter.Value>                                <ControlTemplate TargetType="ListBoxItem">                                    <Border Background="{TemplateBinding Background}">                                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                                                      TextBlock.Foreground="{TemplateBinding Foreground}"/>                                    </Border>                                </ControlTemplate>                            </Setter.Value>                        </Setter>                    </Style>                </Setter.Value>            </Setter>        </Style>                <XmlDataProvider x:Key="personInfo" Source="XMLFile1.xml" XPath="/personInformation"/>    </Window.Resources>      <Grid>        <ListBox ItemsSource="{Binding Source={StaticResource personInfo},XPath=person}"                  ItemTemplate="{StaticResource listboxDataTempPlateStyle}"/>        <!--ItemTemplate="{StaticResource listboxDataTempPlateStyle}" Template="{StaticResource ct}" -->    </Grid></Window>

  

新建XMLFile1.xml文件

<?xml version="1.0" encoding="utf-8" ?><personInformation>  <person>    <Name>张三</Name>    <Age>20</Age>    <Sex>女</Sex>  </person>  <person>    <Name>李四</Name>    <Age>20</Age>    <Sex>男</Sex>  </person>  <person>    <Name>王五</Name>    <Age>20</Age>    <Sex>女</Sex>  </person></personInformation>

  

WPF listbox数据绑定