我有一个由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 时,它没有显示上下文菜单。
答案 0 :(得分:4)
当您右键单击打开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>