Xaml 右键单击GridView项时显示AppBar

Xaml 右键单击GridView项时显示AppBar,xaml,windows-runtime,windows-store-apps,winrt-xaml,appbar,Xaml,Windows Runtime,Windows Store Apps,Winrt Xaml,Appbar,在WinRT应用程序中,右键单击ListBoxItem时,将显示AppBar。但右键单击GridViewItem时,AppBar不会显示。这可以配置吗 如果不是,最好使用列表框而不是GridView并自定义模板。或者我应该用rightstapped命令来实现它。 (我使用MVVM Light,因为Caliburn.Micro当前不工作) RightTappedCommand的示例: public sealed class RightTapped { #region

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

如果不是,最好使用列表框而不是GridView并自定义模板。或者我应该用rightstapped命令来实现它。 (我使用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即可。

这取决于gridview选择模式,右键单击用于选择项目。如果您将selectionmode属性设置为None,则appbar将在右键单击时打开。

因此,在MVVM方式中,它将只是视图模型上的一个属性。绑定到AppBarIt的IsOpen属性取决于您如何接近MVVM,但我想您可以创建一个附加行为(在混合行为上使用纯附加依赖属性)连接到ListBoxItem/按钮,该按钮通过关闭关联的AppBar来处理单击/点击。我只想将RightTapped事件处理程序添加到关闭AppBar的列表项中的一个控件。简单、灵活,不会对MVVM造成太大破坏。这是一个定制的交互逻辑,您无论如何都不可能进行单元测试,也不会将其移植到其他平台等。