gpt4 book ai didi

c# - 如何获取 GridViewColumnHeader Click 事件上的 ListViewControl

转载 作者:行者123 更新时间:2023-12-02 09:50:04 27 4
gpt4 key购买 nike

我有 7 个不同的 ListView。

有一个名为 SortClick(object sender, RoutedEventArgs e) 的 Sort 方法 -当单击 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 事件时,我不太确定您的意思,但我猜您的意思是附加了 SortClick 处理程序的 GridViewColumnHeader.Click 事件。

如果将 GridViewColumnHeader.Click 添加到 ListView,如下所示:

<ListView GridViewColumnHeader.Click="SortClick">

您将在事件处理程序的 sender 参数中获得 ListView 控件,并在 e.OriginalSource< 中获得 GridViewColumnHeader/.

为了清楚起见,请进行编辑。在您的 SortClick 处理程序中,您将像这样访问该控件:

ListView listView = sender as ListView;
GridViewColumnHeader header = e.OriginalSource as GridViewColumnHeader;

关于c# - 如何获取 GridViewColumnHeader Click 事件上的 ListViewControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9157617/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com