我有这个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;
}
ListView.View
当使用如下定义时它运行良好DisplayMemberBinding
:
<!-- 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 代码,但找不到绑定。