概要

WPF の XamGantt コントロールの Grid セクションに配置されている ProjectTableGrid の列に対して、コードからソートを実行する方法について説明します。

対象コントロール

  • Infragistics WPF — XamGantt

問題

XamGantt の Grid セクションに表示されている列(例:”開始” 列)を、ユーザー操作ではなくコードから並べ替えたい場合、どのようにアクセスすればよいかが分かりにくいことがあります。

解説

ProjectTableGrid を取得し、列の IsSorted プロパティを設定する

XamGantt の Grid セクションは内部的に ProjectTableGrid で構成されています。Utilities.GetDescendantFromType メソッドを使用して XamGantt のビジュアルツリーから ProjectTableGrid を取得し、目的の列の IsSorted プロパティに SortDirection 列挙体の値を設定することで、コードからソートを実行できます。

手順は以下のとおりです。

  1. Utilities.GetDescendantFromType で XamGantt から ProjectTableGrid を取得
  2. tableGrid.Columns.DataColumns からソート対象の列を HeaderText で検索
  3. 取得した列の IsSorted プロパティに SortDirection.Ascending(昇順)または SortDirection.Descending(降順)を設定

サンプルコード

XAML

<Button Content="開始日でソート" Click="SortByStart_Click" />

C#

private void SortByStart_Click(object sender, RoutedEventArgs e)
{
    // XamGantt コントロールから ProjectTableGrid を取得
    ProjectTableGrid? tableGrid =
        Utilities.GetDescendantFromType(xamGantt, typeof(ProjectTableGrid), true)
        as ProjectTableGrid;

    if (tableGrid != null)
    {
        // "開始" 列を取得してソート方向をトグル
        Column? col = tableGrid.Columns.DataColumns.FirstOrDefault(x => x.HeaderText == "開始");
        if (col != null)
        {
            col.IsSorted = col.IsSorted == SortDirection.Ascending
                ? SortDirection.Descending
                : SortDirection.Ascending;
        }
    }
}

まとめ

項目コード説明
ProjectTableGrid の取得Utilities.GetDescendantFromType(xamGantt, typeof(ProjectTableGrid), true)XamGantt のビジュアルツリーから ProjectTableGrid を取得する。
列の検索tableGrid.Columns.DataColumns.FirstOrDefault(x => x.HeaderText == “開始”)HeaderText を指定して対象列を取得する。
ソートの設定col.IsSorted = SortDirection.AscendingIsSorted プロパティに SortDirection 列挙体の値を設定してソートを実行する。
Tagged:

製品について

Ultimate UI for WPF