码迷,mamicode.com
首页 > 其他好文 > 详细

WPF listbox数据绑定

时间:2014-10-06 02:27:39      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   ar   for   文件   数据   

<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="True">
                    <Setter Property="Background" Value="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数据绑定

标签:style   blog   http   io   os   ar   for   文件   数据   

原文地址:http://www.cnblogs.com/dywfight/p/4007813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!