我有7个不同的ListViews。
有一种名为SortClick(object sender, RoutedEventArgs e)
的排序方法-
当单击GridViewColumnHeader时,将引发SortClick事件。
但是在这种方法中,我必须告诉哪个ListView应该排序,b.e
listview2.SortNow();
现在我的问题是,如何从GridViewColumnHeader(
sender
)或e
获取控件(ListView),这甚至可能吗?ListView XAML的外观如下:
<ListView ItemsSource="{Binding MissingTables}" Name="missingTablesListView" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="0" MinHeight="540" HorizontalContentAlignment="Stretch">
<ListView.Background>
<ImageBrush />
</ListView.Background>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding TableName}" Width="Auto">
<GridViewColumnHeader Click="SortClick" Content="TableName" Tag="TableName" ToolTip="Sortieren" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Beschreibung}" Width="Auto">
<GridViewColumnHeader Click="SortClick" Content="Description" Tag="Beschreibung" ToolTip="Sortieren" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
这是SortClick代码:
/// <summary>
/// Sortiert (Ascending/Descending)
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">e</param>
private void SortClick(object sender, RoutedEventArgs e)
{
//ListView listView = sender as ListView;
GridViewColumnHeader column = sender as GridViewColumnHeader;
String field = column.Tag as String;
if (currentSortColumn != null)
{
AdornerLayer.GetAdornerLayer(currentSortColumn).Remove(sorter);
listView.Items.SortDescriptions.Clear();
}
ListSortDirection newDir = ListSortDirection.Ascending;
if (currentSortColumn == column && sorter.Direction == newDir)
{
newDir = ListSortDirection.Descending;
}
currentSortColumn = column;
sorter = new Sorter(currentSortColumn, newDir);
AdornerLayer.GetAdornerLayer(currentSortColumn).Add(sorter);
listView.Items.SortDescriptions.Add(new SortDescription(field, newDir));
}
最佳答案
我不太确定您说的SortClick事件是什么意思,但我想您的意思是附加了GridViewColumnHeader.Click
处理程序的SortClick
事件。
如果像这样将GridViewColumnHeader.Click
添加到ListViews:
<ListView GridViewColumnHeader.Click="SortClick">
您将在事件处理程序的
ListView
参数中获得sender
控件,在GridViewColumnHeader
中获得e.OriginalSource
。编辑为清楚起见。在
SortClick
处理程序中,您将像这样访问控件:ListView listView = sender as ListView;
GridViewColumnHeader header = e.OriginalSource as GridViewColumnHeader;