c# - 如果 ListView 不是 "None"选择模式,则 RightTapped 不会在 Metro ListViewItem 上触发

标签 c# xaml windows-8 windows-runtime winrt-xaml

问题实际上与此处描述的相同:http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/542b827a-7c8e-4984-9158-9d8479b2d5b1但我对那里接受的答案并不满意,觉得必须有更好的解决方案......

我正在尝试在我的 ListView 上实现一个“经典”上下文菜单(不是通过 AppBar,而是通过 PopupMenu)并且效果很好,但前提是我将 ListView 设置为“无”选择模式。

上面的链接正确地解释了如果 ListView 设置为“无”选择模式以外的其他选择模式,ListViewItem 会“吞噬”右侧的点击事件。我如何覆盖此行为,以便选择 ListView 项并且 ListView 仍会触发我可以对其使用react的 RightTapped 事件?

最佳答案

我相信我通过子类化 ListView 和 ListViewItem 类解决了这个问题。

public class MyListView : ListView
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new MyListViewItem();
    }
}

public class MyListViewItem : ListViewItem
{
    protected override void OnRightTapped(Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
    {
        base.OnRightTapped(e);
        e.Handled = false; // Stop 'swallowing' the event
    }
}

<CustomControls:MyListView
    x:Name="MyListView"
    ...
    SelectionMode="Single"
    RightTapped="MyListView_RightTapped">
</CustomControls:MyListView>

通过这种简单的方法,即使 SelectionMode 设置为“Single”、“Multiple”或“Extended”,MyListView_RightTapped 处理程序也会被调用。然而,下一个问题是这个小胜利是否会导致应用程序 UI 的良好设计。我不会在这里尝试回答这个问题......

关于c# - 如果 ListView 不是 "None"选择模式,则 RightTapped 不会在 Metro ListViewItem 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10490709/

相关文章:

c# - dotnet 核心 Entity Framework 连接已关闭

c# - 与时间无关的日期列表

wpf - 向提示 "Window is not supported in a WPF project"添加新的IronPython WPF窗口

.net - 如何在 Windows 8 中自动运行需要管理员权限的应用程序

windows-8 - 如何在 WinRT XAML 中拥有 DesignTime 数据?

xaml - Windows 8 主题颜色 - 以编程方式访问它

c# - 使用服务器更新 PC 时间 - C#

c# - 将 MyDbContext 合并到 Asp.net IdentityDbContext

wpf - 创建具有不同前后拇指背景颜色的 slider

xaml - UWP 中的 TextBlock 背景属性去哪儿了?