2012-06-16 15 views
0

美好的一天!WPF:在运行时排序XMLdataprovider

我已经工作了很长时间来创建我自己的HTPC应用程序,它基本上允许我简单地浏览和播放我的电影集合。我编写了另一个小应用程序索引所有内容,并准备一个XML文件,其中包含您在下面看到的布局。

这一切工作正常,但现在我想实施一些排序选项。通过按“S”键,我想循环一些固定的排序方法,如“按标题”,“按发布日期”,“按流派”,...

我的XAML是事物的混搭我在网上找到了,不幸的是,我不是专家,它显示在杂乱的代码中。它基本上将fanart作为背景,并在屏幕底部水平滚动条,您可以选择电影(它也会将选定的电影保留在屏幕中间)。

我该如何解决这个问题?

<MOVIES> 
    <MOVIE> 
    <TITLE></TITLE> 
    <PATH></PATH> 
    <FANART></FANART> 
    <COVER></COVER> 
    <RELEASEDATE></RELEASEDATE> 
    etc... 
    </MOVIE> 
</MOVIES> 


<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" WindowState="Maximized" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="350" Width="525" ShowInTaskbar="True" WindowStyle="None" Loaded="Window_Loaded"> 
<Window.Resources> 
    <XmlDataProvider x:Key="MovieData" 
       Source="movies.xml" /> 

    <DataTemplate x:Key="ItemTemplate"> 
     <Image Source="{Binding XPath=COVER}" Width="166" Height="250" Margin="0,0" MaxWidth="166" MaxHeight="250" MinWidth="166" MinHeight="250" Stretch="Fill" /> 
    </DataTemplate> 

    <DataTemplate x:Key="SelectedItemTemplate"> 
     <Border BorderBrush="WhiteSmoke" BorderThickness="3"> 
      <Image Source="{Binding XPath=COVER}" Width="250" Height="375" Margin="0,0" MaxWidth="250" MaxHeight="375" MinWidth="250" MinHeight="375" Stretch="Fill" /> 
     </Border> 
    </DataTemplate> 

    <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle"> 
     <Style.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
     </Style.Resources> 
     <Setter Property="FocusVisualStyle"> 
      <Setter.Value> 
       <Style /> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" /> 
     <Style.Triggers> 
      <Trigger Property="IsSelected" Value="True"> 
       <Setter Property="ContentTemplate" Value="{StaticResource SelectedItemTemplate}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

</Window.Resources> 
<Grid DataContext="{Binding Source={StaticResource MovieData}, XPath=/MOVIES/MOVIE}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="178*" /> 
     <RowDefinition Height="133*" /> 
    </Grid.RowDefinitions> 
    <Grid.Background> 
     <ImageBrush ImageSource="{Binding XPath=FANART}"/> 
    </Grid.Background> 
    <ListBox x:Name="Listbox1" 
      SelectionChanged="ScrollIntoView" 
      ItemContainerStyle="{StaticResource ContainerStyle}" 
      IsSynchronizedWithCurrentItem="True" 
      ItemsSource="{Binding}" 
      HorizontalAlignment="Center" Grid.Row="1" VerticalAlignment="Bottom" KeyDown="Listbox1_KeyDown"> 
     <ListBox.Style> 
      <Style TargetType="ListBox"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type ListBox}"> 
          <Border> 
           <ScrollViewer x:Name="ScrollView1" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" VerticalContentAlignment="Bottom" CanContentScroll="True"> 
            <VirtualizingStackPanel x:Name="SPanel1" IsItemsHost="True" Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center"/> 
           </ScrollViewer> 
          </Border> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </ListBox.Style> 
    </ListBox> 
</Grid> 

回答

2

在为列表框KeyDown事件处理程序,你需要这样一个电话:

Listbox1.Items.SortDescriptions.Clear(); 
Listbox1.Items.SortDescriptions.Add(new SortDescription("TITLE", ListSortDirection.Descending)); 

这增加了一种描述,当你绑定的ItemsSource创建的默认集合视图。你也可以这样过滤。

排序描述中的属性文本可以是XPath选择器。

+0

非常感谢你,它就像一个魅力! –