我怎样才能获得GridViewColumnHeader上的ListViewControl Click事件

时间:2012-02-06 08:53:31

标签: c# wpf listview sender

我有7种不同的ListView。

有一种称为SortClick(object sender, RoutedEventArgs e)的Sortmethod - 单击GridViewColumnHeader时,它会引发SortClick事件。

但是在这个方法中我必须告诉,哪个ListView应该被排序,b.e

listview2.SortNow();

现在我的问题是,如何从GridViewColumnHeader(sender)或e获取Control(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));
        }

2 个答案:

答案 0 :(得分:4)

当你说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;

答案 1 :(得分:2)

我会选择克莱门斯的解决方案,但如果它不是你的选择,你也可以走在可视树上找到ListView:

    static T FindAncestor<T>(DependencyObject obj) where T : DependencyObject
    {
        var tmp = VisualTreeHelper.GetParent(obj);
        while (tmp != null && !(tmp is T))
        {
            tmp = VisualTreeHelper.GetParent(tmp);
        }
        return (T)tmp;
    }

    ...

    var listView = FindAncestor<ListView>(columnHeader);