gpt4 book ai didi

wpf - 命令绑定(bind)到 ContextMenu(在 ListBox 中的 ListBoxItem 上)不起作用

转载 作者:行者123 更新时间:2023-12-02 04:34:20 27 4
gpt4 key购买 nike

在 WPF 中,通过 MVVM 轻量级,有一个 Class (由一些学生组成),以及 Class持有一些Student s。

enter image description here

右键单击一个学生的名字,然后会显示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);
}
}
}
}

最佳答案

您需要一个代理将命令绑定(bind)到列表框项目的上下文菜单。请参阅此处的答案:

http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

关于wpf - 命令绑定(bind)到 ContextMenu(在 ListBox 中的 ListBoxItem 上)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19447795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com