2011年3月4日 星期五

WPF4-使用CollectionViewSource排序、篩選資料

我們可以使用這段XAML呼叫System.IO下Directory類別的EnumerateFiles方法將C:\下所有檔案列在ListBox

<Window x:Class="CollectionViewSourceDemo.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300"
        xmlns:io="clr-namespace:System.IO;assembly=mscorlib"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        >
    <Window.Resources>
        <ObjectDataProvider x:Key="myProvider"
                            ObjectType="{x:Type io:Directory}" MethodName="EnumerateFiles">
            <ObjectDataProvider.MethodParameters>
                <sys:String>C:\</sys:String>              
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource myProvider}}"/>
    </Grid>
</Window>

執行結果參考如下

image

改用CollectionViewSource 設排序方向性為Descending

<Window x:Class="CollectionViewSourceDemo.Window3"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300"
        xmlns:io="clr-namespace:System.IO;assembly=mscorlib"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:d="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        >
    <Window.Resources>
        <ObjectDataProvider x:Key="myProvider"
                            ObjectType="{x:Type io:Directory}" MethodName="EnumerateFiles">
            <ObjectDataProvider.MethodParameters>
                <sys:String>C:\</sys:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <CollectionViewSource x:Key="myViewSource" Source="{StaticResource myProvider}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription    Direction="Descending" />
            </CollectionViewSource.SortDescriptions>

        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource myViewSource}}"/>
    </Grid>
</Window>

加上Filter事件

<Window x:Class="CollectionViewSourceDemo.Window4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300"
        xmlns:io="clr-namespace:System.IO;assembly=mscorlib"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:d="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        >
    <Window.Resources>
        <ObjectDataProvider x:Key="myProvider"
                            ObjectType="{x:Type io:Directory}" MethodName="EnumerateFiles">
            <ObjectDataProvider.MethodParameters>
                <sys:String>C:\</sys:String>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <CollectionViewSource            
            x:Key="myViewSource" Source="{StaticResource myProvider}"
            Filter="CollectionViewSource_Filter"
            >
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription    Direction="Descending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource myViewSource}}"/>
    </Grid>
</Window>

然後在cs檔案中,加入

private void CollectionViewSource_Filter(object sender, FilterEventArgs e) {
           string f = e.Item.ToString();
           if (f.EndsWith(".txt"))
               e.Accepted = true;
           else
               e.Accepted = false;
       }

就可以篩選副檔名為.txt檔案出來

image

沒有留言:

總網頁瀏覽量