WPF:在运行时对XMLdataprovider进行排序

时间:2012-06-16 18:53:06

标签: c# wpf sorting xmldataprovider

美好的一天!

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

一切正常,但现在我想实现一些排序选项。按下“S”键我想迭代一些固定的排序方法,比如“By Title”,“By Release Date”,“By Genre”,......

我的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>

1 个答案:

答案 0 :(得分:2)

在listbox的KeyDown事件处理程序中,您需要这样的调用:

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

这会将绑定描述添加到绑定到ItemsSource时创建的默认集合视图中。你也可以用这种方式过滤。

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