3

WPF DataGridの列ヘッダーの並べ替えマーカーがCollectionViewSourceの現在の並べ替え順序と同期されるようにするには(できれば純粋にXAMLで)どのように達成しますか?

たとえば、C:\内のすべてのファイルの属性を長さで並べ替えて表示する次のサンプルコードがあります。

<Window x:Class="DataGridSortTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cm="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <CollectionViewSource x:Key="items" Source="{Binding Files}">
            <CollectionViewSource.SortDescriptions>
                <cm:SortDescription PropertyName="Length"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <DataGrid ItemsSource="{Binding Source={StaticResource items}}"/>
    </Grid>
</Window>

MainWindow.xaml.csのコードビハインドは次のとおりです。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Files = Directory.GetFiles(@"C:\").Select(f => new FileInfo(f)).ToList();

        DataContext = this;
    }

    public IEnumerable<FileInfo> Files { get; private set; } 
}

このプログラムを開始すると、次のようになります。

スクリーンショット

つまり、ファイルは正しくソートされていますが、ヘッダーのマーカーがありません。「マーカー」とは、次のことを意味します。

並べ替えマーカー

:私は一般的な解決策を探しています。設定するだけDataGridColumn.SortDirectionでは解決策にはなりません。コレクションビューからソート順を自動的に取得するようにDataGridに指示する方法を探しています。

4

3 に答える 3

0

あなたの一般的な解決策について、私は2つの提案をします。

初め:

新しいDataGridクラスを作成し、SortDescriptionsを自動的にチェックできます。

 class ExDataGrid : DataGrid
    {
        bool isFirstRow = true;

        protected override void OnLoadingRow(DataGridRowEventArgs e)
        {
            base.OnLoadingRow(e);
            if (isFirstRow)
                SortDirectionHelper(this);
        }

        private void SortDirectionHelper(DataGrid dg)
        {
            var tmp = dg.Items.SortDescriptions;

            foreach (SortDescription sd in tmp)
            {
                var col = dg.Columns.Where(x => (((Binding)x.ClipboardContentBinding).Path.Path == sd.PropertyName)).FirstOrDefault();
                if (col != null)
                {
                    col.SortDirection = sd.Direction;
                }
            }

            isFirstRow = false;
        }
    }

Xamlファイル:

<Window x:Class="DataGridSortTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cm="clr-namespace:System.ComponentModel;assembly=WindowsBase" 
        Title="MainWindow" Height="350" Width="525"
        xmlns:dg="clr-namespace:DataGridSortTest"
        >
    <Window.Resources>
        <CollectionViewSource x:Key="items" Source="{Binding Files}" >
            <CollectionViewSource.SortDescriptions>
                <cm:SortDescription PropertyName="Length"/>
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>      
        <dg:ExDataGrid x:Name="grid" ItemsSource="{Binding Source={StaticResource items}}" />
    </Grid>
</Window>

2番:

すべてのグリッドでLoadedイベントのEventHandlerを作成できます

 private void grid_Loaded(object sender, RoutedEventArgs e)
        {
            SortDirectionHelper(sender as DataGrid);
        }

        public void SortDirectionHelper(DataGrid dg)
        {
            var tmp = dg.Items.SortDescriptions;

            foreach (SortDescription sd in tmp)
            {
                var col = dg.Columns.Where(x => (((Binding)x.ClipboardContentBinding).Path.Path == sd.PropertyName)).FirstOrDefault();
                if (col != null)
                {
                    col.SortDirection = sd.Direction;
                }
            }
        }
于 2012-07-16T19:01:39.907 に答える
0

msdn フォーラムから

マーカー機能はバインディング ソースではない DataGrid のものであるため、列ヘッダーにマーカーを表示するには、DataGrid 列の SortDirection プロパティを明示的に指定する必要があります。

@kmatyaszekソリューションを使用する方が良いかもしれません

于 2012-07-17T07:53:36.830 に答える