xaml - 右键单击 GridView 项目时显示 AppBar

标签 xaml windows-runtime windows-store-apps winrt-xaml appbar

在 WinRT 应用程序中,当您右键单击 ListBoxItem 时,将显示 AppBar。但是,当您右键单击 GridViewItem 时,AppBar 不会出现。这个可以配置吗?

如果不是,最好使用 ListBox 而不是 GridView 并自定义模板。或者我应该使用 RightTapped 命令来实现它。 (我使用 MVVM Light,因为 Caliburn.Micro 目前无法工作)

RightTappedCommand 示例:



 public sealed class RightTapped
    {
        #region Properties

        #region Command

        /// 
        /// GetCommand
        /// 
        /// 
        /// 
        public static ICommand GetCommand(DependencyObject obj)
        {
            return (ICommand)obj.GetValue(CommandProperty);
        }

        /// 
        /// SetCommand
        /// 
        /// 
        /// 
        public static void SetCommand(DependencyObject obj, ICommand value)
        {
            obj.SetValue(CommandProperty, value);
        }

        /// 
        /// DependencyProperty CommandProperty
        /// 
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(RightTapped), new PropertyMetadata(null, OnCommandChanged));

        #endregion Command

        #region CommandParameter

        /// 
        /// GetCommandParameter
        /// 
        /// 
        /// 
        public static object GetCommandParameter(DependencyObject obj)
        {
            return (object)obj.GetValue(CommandParameterProperty);
        }

        /// 
        /// SetCommandParameter
        /// 
        /// 
        /// 
        public static void SetCommandParameter(DependencyObject obj, object value)
        {
            obj.SetValue(CommandParameterProperty, value);
        }

        /// 
        /// DependencyProperty CommandParameterProperty
        /// 
        public static readonly DependencyProperty CommandParameterProperty =
            DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(RightTapped), new PropertyMetadata(null, OnCommandParameterChanged));

        #endregion CommandParameter

        #region HasCommandParameter

        private static bool GetHasCommandParameter(DependencyObject obj)
        {
            return (bool)obj.GetValue(HasCommandParameterProperty);
        }

        private static void SetHasCommandParameter(DependencyObject obj, bool value)
        {
            obj.SetValue(HasCommandParameterProperty, value);
        }

        private static readonly DependencyProperty HasCommandParameterProperty =
            DependencyProperty.RegisterAttached("HasCommandParameter", typeof(bool), typeof(RightTapped), new PropertyMetadata(false));

        #endregion HasCommandParameter

        #endregion Propreties

        #region Event Handling

        private static void OnCommandParameterChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            SetHasCommandParameter(o, true);
        }

        private static void OnCommandChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            var element = o as FrameworkElement;
            if (element != null)
            {
                if (e.NewValue == null)
                {
                    element.RightTapped -= FrameworkElementKeyUp;
                }
                else if (e.OldValue == null)
                {
                    element.RightTapped += FrameworkElementKeyUp;
                }
            }
        }

        private static void FrameworkElementKeyUp(object sender, RightTappedRoutedEventArgs e)
        {
            var o = sender as DependencyObject;
            var command = GetCommand(sender as DependencyObject);

            var element = e.OriginalSource as FrameworkElement;
            if (element != null)
            {
                // If the command argument has been explicitly set (even to NULL) 
                if (GetHasCommandParameter(o))
                {
                    var commandParameter = GetCommandParameter(o);

                    // Execute the command 
                    if (command.CanExecute(commandParameter))
                    {
                        command.Execute(commandParameter);
                    }
                }
                else if (command.CanExecute(element.DataContext))
                {
                    command.Execute(element.DataContext);
                }
            }
        }

        #endregion
    }

在 Xaml 中,类似这样:

common:Tapped.Command="{Binding ShowAppBar}"

最佳答案

您只需执行 myAppBar.IsOpen = true 即可。

关于xaml - 右键单击 GridView 项目时显示 AppBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12079086/

相关文章:

WPF 松散 XAML 资源字典

c# - 在 FlipView 的 KeyDown 事件中处理方向键

c# - WPF GridView : Determining held item

c# - scrollviewer 只缩放一个元素

wpf - 如何在 XAML 编辑器中查看设计时数据绑定(bind)(它在运行时工作)?

wpf - 数据绑定(bind)在 xaml 中不起作用

javascript - 由于搜索尚未运行和激活时如何调试应用程序

c# - 当缩放中心因捏合(缩放)或 metro 风格应用程序中的其他手势而发生变化时,如何在 Canvas 上缩放时避免出现 "jump"

c# - 需要知道当另一个元素失去焦点时哪个元素获得焦点

c# - 编译 C++ 源代码以在 Windows 应用商店应用程序 (Windows 8) 中使用它们