在WPF中,使用MVVM指示灯,有一个Class
(由一些学生组成),而Class
包含一些Student
。
右键单击一个学生的姓名,然后显示MessageBox
,这样就可以了:
ClassDetailView.xaml
<UserControl DataContext="{Binding ClassDetail, Source={StaticResource Locator}}">
<DockPanel>
<ListBox
ItemsSource="{Binding Students}"
DisplayMemberPath="Name">
<ListBox.ContextMenu>
<ContextMenu DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
<MenuItem
Header="Show Selected"
Command="{Binding Path=DataContext.RemoveStudentCommand}"
CommandParameter="{Binding Path=SelectedItem}"/>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
</DockPanel>
</UserControl>
但是,它不能以这种方式工作(使用ListBox.ItemContainerStyle):
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Show Selected"
Command="{Binding Path=DataContext.RemoveStudentCommand}"
CommandParameter="{Binding Path=SelectedItem}"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
而不是
<ListBox.ContextMenu>
<ContextMenu ...>
...
<ContextMenu />
</ListBox.ContextMenu>
ClassDetailViewModel.cs
namespace ContextMenu.ViewModel
{
public class ClassDetailViewModel : ViewModelBase
{
public ClassDetailViewModel()
{
CreateData();
}
public void CreateData()
{
students.Add(new StudentViewModel() { Name = "QQ" });
students.Add(new StudentViewModel() { Name = "WW" });
students.Add(new StudentViewModel() { Name = "EE" });
students.Add(new StudentViewModel() { Name = "RR" });
students.Add(new StudentViewModel() { Name = "AA" });
students.Add(new StudentViewModel() { Name = "SS" });
students.Add(new StudentViewModel() { Name = "DD" });
students.Add(new StudentViewModel() { Name = "FF" });
students.Add(new StudentViewModel() { Name = "ZZ" });
students.Add(new StudentViewModel() { Name = "XX" });
}
public const string StudentsPropertyName = "Students";
private ObservableCollection<StudentViewModel> students =
new ObservableCollection<StudentViewModel>();
public ObservableCollection<StudentViewModel> Students
{
get { return students; }
set
{
if (students == value) { return; }
students = value;
RaisePropertyChanged(StudentsPropertyName);
}
}
private RelayCommand<StudentViewModel> removeStudentCommand;
public RelayCommand<StudentViewModel> RemoveStudentCommand
{
get
{
return removeStudentCommand
?? (removeStudentCommand =
new RelayCommand<StudentViewModel>(ExecuteRemoveStudentCommand));
}
}
private void ExecuteRemoveStudentCommand(StudentViewModel student)
{
if (null == student) { return; }
MessageBox.Show(string.Format("RemoveStudent:{0}", student.Name));
}
}
}
StudentViewModel.cs
namespace ContextMenu.ViewModel
{
public class StudentViewModel : ViewModelBase
{
public const string NamePropertyName = "Name";
private string name = "";
public string Name
{
get { return name; }
set
{
if (name == value) { return; }
name = value;
RaisePropertyChanged(NamePropertyName);
}
}
}
}
答案 0 :(得分:3)
您可以将contextmenu用作资源吗?
类似的东西:
<UserControl.Resources>
<ContextMenu x:name="contextMenuExample" DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Show Selected"
Command="{Binding Path=DataContext.RemoveStudentCommand}"
CommandParameter="{Binding Path=SelectedItem}"/>
</ContextMenu>
</UserControl.Resources>
然后在列表中,执行以下操作:
<Listbox ContextMenu = {StaticResource contextMenuExample} ... />
或者你真的想使用ItemContainerStyle吗?
from - &gt; how to right click on item from Listbox and open menu on WPF
<ListBox Name="someListBox" MouseDown="someListBox_MouseDown">
<ListBox.Resources>
<!--Defines a context menu-->
<ContextMenu x:Key="MyElementMenu">
<MenuItem Header="Delete" Click="MenuItemDelete_Click"/>
</ContextMenu>
<!--Sets a context menu for each ListBoxItem in the current ListBox-->
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ContextMenu" Value="{StaticResource MyElementMenu}"/>
</Style>
</ListBox.Resources>
<ListBoxItem>...</ListBoxItem>
<ListBoxItem>...</ListBoxItem>
<ListBoxItem>...</ListBoxItem>
</ListBox>
答案 1 :(得分:3)
您需要一个代理才能将命令绑定到listboxitem的上下文菜单。请参阅答案:
答案 2 :(得分:1)
通过将ContextMenu移动到ListBoxItem,您已将DataContext从ClassDetailsViewModel(ListBox的DataContext)更改为StudentViewModel(ListBoxItem的DataContext)。因此,您需要更改路径以访问父ListBox的DataContext以访问RelayCommand。
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=DataContext}">
<MenuItem Header="Show Selected"
Command="{Binding Path=RemoveStudentCommand}"
CommandParameter="{Binding Path=SelectedItem}"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>