如何从 GridViewColumn 标题获取点击事件? [英] How do I get a click event from a GridViewColumn header?

查看:43
本文介绍了如何从 GridViewColumn 标题获取点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从标题下的所有内容获取事件,但无法通过单击标题获取事件.这是 XAML;注意该事件是针对整个 ListView 的,因此它应该在单击任何内容时激活:

I can get events from everything under the headers, but I can't get an event from clicking the headers. Here's the XAML; notice the event is for the entire ListView, so it should activate when anything is clicked:

<ListView x:Name="myListView" MouseLeftButtonUp="myListView_MouseLeftButtonUp" Margin="10">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="File Path"
                                DisplayMemberBinding="{Binding Path=Path}"  />
                <GridViewColumn Header="File Size"
                                DisplayMemberBinding="{Binding Path=Size}" />
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

而且事件本身非常简单.告诉我发生了什么事:

And the event itself is very simple. Just show me that something has happened:

    private void myListView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        string output = sender.ToString();
        MessageBox.Show(output);
    } 

点击标题下的任何地方都能完美响应:
System.Windows.Controls.ListView Items.Count:0"

单击文件路径"标题什么也不做.单击文件大小"标题没有任何作用.

Clicking anywhere under the headers responds perfectly:
"System.Windows.Controls.ListView Items.Count:0"

Clicking the "File Path" header does nothing. Clicking the "File Size" header does nothing.

MSDN 说:
https://msdn.microsoft.com/en-us/library/vstudio/ms745786(v=vs.100).aspx

<ListView x:Name='lv' Height="150" HorizontalAlignment="Center" VerticalAlignment="Center" 
GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">

Visual Studio 说没有 GridViewColumnHeader 这样的东西,所以 MSDN 上的代码都不起作用.

Visual Studio says there's no such thing as GridViewColumnHeader, so none of the code on MSDN works.

推荐答案

这就是 WPF UI 事件默认的工作方式.他们冒泡.如果有人在此过程中吃掉了消息(按钮类型控件就是这样做的),则更高级别的控件将不会得到它.您可以使用活动的预览版,也可以使用更简洁的方式:

That's how WPF UI events work by default. They bubble up. If somebody eats the message along the way (which is what button type controls do), the higher level controls won't get it. You can either use the Preview version of the event, or the cleaner way to do it:

AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ListView_OnColumnClick));

这篇关于如何从 GridViewColumn 标题获取点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆