WPF DataGridColumn DisplayMemeberBinding

WPF DataGridColumn DisplayMemeberBinding

我有这个ListView排序程序:

    /// <summary>
    /// Handle the event of clicking on a column header to sort
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void GridViewColumnHeaderClicked(object sender, RoutedEventArgs e)
    {
        GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;

        if (headerClicked == null)
            return;

        if (headerClicked.Role == GridViewColumnHeaderRole.Padding)
            return;

>>>     var sortingColumn = (headerClicked.Column.DisplayMemberBinding as Binding)?.Path?.Path;
        if (sortingColumn == null)
            return;

        var direction = ApplySort(Items, sortingColumn);

        if (direction == ListSortDirection.Ascending)
        {
            headerClicked.Column.HeaderTemplate =
                Resources["HeaderTemplateArrowUp"] as DataTemplate;
        }
        else
        {
            headerClicked.Column.HeaderTemplate =
                Resources["HeaderTemplateArrowDown"] as DataTemplate;
        }

        // Remove arrow from previously sorted header
        if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
        {
            _lastHeaderClicked.Column.HeaderTemplate =
                Resources["HeaderTemplateDefault"] as DataTemplate;
        }

        _lastHeaderClicked = headerClicked;
    }

当使用 DisplayMemberBinding 定义 ListView.View 时效果很好,如下所示:

    <!-- Name -->
    <GridViewColumn Width="200" 
                    DisplayMemberBinding="{Binding FullName}">
        <GridViewColumn.Header>
        <GridViewColumnHeader Content=" Name"
                              HorizontalContentAlignment="Left" />
        </GridViewColumn.Header>
    </GridViewColumn>

我现在需要使一些列右对齐,所以我必须使用这样的东西:

<!-- Name -->
<GridViewColumn Width="200">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding FullName}"
                       TextAlignment="Right" />
            </DataTemplate>
    </GridViewColumn.CellTemplate>
    <GridViewColumn.Header>
    <GridViewColumnHeader Content=" Name"
                          HorizontalContentAlignment="Left" />
    </GridViewColumn.Header>
</GridViewColumn>

但是,我现在不再有 DisplayMemberBinding 来参考排序过程(标有 >>> 的行)。有没有办法从 CellTemplate -> TextBlock 中找到绑定?我一直在使用 Snoop 挖掘 C# 代码和 XAML 代码,但无法找到绑定。

我会亲自写一个附件属性:

public static class GridViewExt
{
    #region GridViewExt.SortColumnPath Attached Property
    public static String GetSortColumnPath(GridViewColumn obj)
    {
        return (String)obj.GetValue(SortColumnPathProperty);
    }

    public static void SetSortColumnPath(GridViewColumn obj, String value)
    {
        obj.SetValue(SortColumnPathProperty, value);
    }

    public static readonly DependencyProperty SortColumnPathProperty =
        DependencyProperty.RegisterAttached("SortColumnPath", typeof(String), typeof(GridViewExt),
            new PropertyMetadata(null, SortColumnPath_PropertyChanged));

    private static void SortColumnPath_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var target = d as GridViewColumn;
    }
    #endregion GridViewExt.SortColumnPath Attached Property
}

用法:

private void GridViewColumnHeaderClicked(object sender, RoutedEventArgs e)
{
    GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;

    if (headerClicked == null)
        return;

    if (headerClicked.Role == GridViewColumnHeaderRole.Padding)
        return;

    //  ...if DisplayMemberPath is null...
    var sortingColumn = GridViewExt.GetSortColumnPath(headerClicked.Column);

    if (sortingColumn == null)
        return;

XAML:

<GridViewColumn 
    Width="200"
    local:GridViewExt.SortColumnPath="FullName"
    >
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding FullName}"
                       TextAlignment="Right" />
            </DataTemplate>
    </GridViewColumn.CellTemplate>

我认为您可以同时使用 DisplayMemberBindingCellTemplate,但如果使用 DisplayMemberBinding,则 CellTemplate 将被忽略。