【发布时间】: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