【问题标题】:Context Menu not showing when pressing Shift+f10 in GridView Column在 GridView 列中按 Shift+f10 时不显示上下文菜单
【发布时间】:2016-01-27 08:57:06
【问题描述】:

我有一个由 GridView 组成的 ListView

Xaml

<Window x:Class="GridViewContextMenu.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GridViewContextMenu"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ContextMenu x:Key="DynamicPeriodContextMenu" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" DataContext="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
            <MenuItem Header="Open" />
            <MenuItem Header="SetAsComparativePeriod"/>
            <Separator/>
            <MenuItem Header="DeletePeriod"/>
            <MenuItem Header="Properties"/>
        </ContextMenu>
    </Window.Resources>
    <Grid>
        <ListView ItemsSource="{Binding Cars}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Grid ContextMenu="{StaticResource DynamicPeriodContextMenu}">
                                    <TextBlock Text="{Binding Name}"/>
                                </Grid>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Model">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Model}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Company">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Company}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Registration">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Registration}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>

代码隐藏:

namespace GridViewContextMenu
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ViewModel vm = new ViewModel();
            this.DataContext = vm;
        }
    }
}

视图模型

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GridViewContextMenu
{
    public class ViewModel : INotifyPropertyChanged
    {
        private System.Collections.ObjectModel.ObservableCollection<Car> mCars;

        public System.Collections.ObjectModel.ObservableCollection<Car> Cars
        {
            get
            {
                if (mCars == null)
                {
                    mCars = new System.Collections.ObjectModel.ObservableCollection<Car>();
                }
                return mCars;
            }
            set
            {
                mCars = value;
                OnPropertyChanged("Cars");
            }
        }
        public ViewModel()
        {
            for (int i = 0; i < 5; i++)
            {
                Car car = new Car();
                car.Company = "Company" + i;
                car.Model = "Model" + i;
                car.Name = "Name" + i;
                car.Registration = "Registration" + i;
                Cars.Add(car);
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

模型类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GridViewContextMenu
{
    public class Car : System.ComponentModel.INotifyPropertyChanged
    {
        private System.String mName;

        public System.String Name
        {
            get { return mName; }
            set
            {
                mName = value;
                OnPropertyChanged("Name");
            }
        }
        private System.String mModel;

        public System.String Model
        {
            get { return mModel; }
            set
            {
                mModel = value;
                OnPropertyChanged("Model");
            }
        }
        private System.String mCompany;

        public System.String Company
        {
            get { return mCompany; }
            set
            {
                mCompany = value;
                OnPropertyChanged("Company");
            }
        }
        private System.String mRegistration;

        public System.String Registration
        {
            get { return mRegistration; }
            set
            {
                mRegistration = value;
                OnPropertyChanged("Registration");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

我写的都是为了通俗易懂,

当我单击名称列单元格时,它显示 DynamicPeriodContextMenu,但是当我按下 Shift + F10 时,它没有显示上下文菜单。

【问题讨论】:

    标签: c# wpf gridview contextmenu


    【解决方案1】:

    当您通过右键单击打开 ContextMenu 时,WPF 会确切知道您单击的位置(在单元格模板中的网格上)并打开与网格关联的 ContextMenu。

    使用 Shift+F10 仅对 ListView 本身的 ContextMenu 有效(开箱即用)。

    例如,如果我在模板中的 Grid 上设置 Focusable = True,然后使用 Tab 将焦点设置在该 Grid 上,那么我可以使用 Shift+F10 就好了。

                    <Grid ContextMenu="{StaticResource DynamicPeriodContextMenu}"
                          Focusable="True">
                        <TextBlock Text="{Binding Name}"/>
                    </Grid>
    

    【讨论】:

    • 有什么方法可以不用点击标签吗?
    • 将 ContextMenu 放在网格单元格中的原因是什么?你想为不同的列使用不同的 ContextMenus 吗?如果是这样,您在使用快捷方式时如何区分列?
    • 这是为不同列设置不同上下文菜单的想法。当用户单击/选择特定列单元格时,上下文菜单应出现在 shift +f10 上。
    • 我猜你是对的,在选择 listviewitem 时出现特定的上下文菜单是没有意义的。谢谢你的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多