【发布时间】:2017-01-04 13:40:03
【问题描述】:
我有三个选项卡,每个选项卡都有一个包含不同类型文件的列表框。
当我右键单击列表框中的项目时,我想要一个带有“新建、编辑和删除”作为项目标题的 ContextMenu。
我想我可以为每个列表框设置一个 ContextMenu,然后为每个标题设置一个单独的方法,例如:
<ListBox.ContextMenu>
<ContextMenu x:Name="NewEditDeleteAdvCalcFileContextMenu">
<MenuItem Name="NewAdv" Header="New" Click="NewAdv_Click" />
<MenuItem Name="EditAdv" Header="Edit" Click="EditAdv_Click"/>
<MenuItem Name="DeleteAdv" Header="Delete" Click="DeleteAdv_Click"/>
</ContextMenu>
</ListBox.ContextMenu>
但真的,我希望有更好的方法。
我看到这篇帖子显示ContextMenu as Static Resource
这似乎是我想做的事情。 在同一个线程中,建议使用命令: ContextMenu with Commands
我希望我能得到被点击的 ListBoxItem 的类型,因为我需要它。新文件类型 B 的处理方式必须与新文件类型 C 不同,但我不想要大量的上下文菜单和新建/编辑/删除方法。
所以,目前我的 xaml 文件中有更高的内容:
<UserControl.Resources>
<ContextMenu x:Key="NewEditDeleteContextMenu">
<MenuItem Header="New"
Command="{Binding Path=NewFileCommand}"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>
<MenuItem Header="Edit"
Command="{Binding Path=EditFileCommand}"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>
<MenuItem Header="Delete"
Command="{Binding Path=DeleteFileCommand}"
CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"/>
</ContextMenu>
</UserControl.Resources>
然后是tabItem中的一个列表框:
<ListBox Name="CalcFilesListBox"
Margin="20" ItemsSource="{Binding CalcFilesList}"
PreviewMouseRightButtonUp="ListBox_PreviewMouseRightButtonUp"
ContextMenu="{StaticResource NewEditDeleteContextMenu}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="MouseDoubleClick" Handler="CalcFileListBox_MouseDoubleClick"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
问题 #1
如何右键单击 ListBoxItem 以显示 ContextMenu,它现在是静态资源? 因为在我的 xaml.cs 我有这个:
private void ListBox_PreviewMouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// SelectItemOnRightClick(e);
NewEditDeleteContextMenu.PlacementTarget = sender as UIElement;
NewEditDeleteContextMenu.IsOpen = true;
}
但现在我有一个错误说:
当前上下文中不存在名称“NewEditDeleteContextMenu”。
因为最初我将上下文菜单作为 ListBox 的一部分,例如:
<ListBox.ContextMenu>
...
但据我所知,这意味着每个 ListBox 都有一个单独的 ContextMenu。
问题 #2
是使用命令的正确方法,假设为 ContextMenu 中的 New 项标题的 NewFileCommand(显示在 UserControl.Resources 代码块中)执行以下操作:
在我的 ViewModel 中:
public RelayCommand<string> NewFileCommand { get; private set; }
然后在 ViewModel 的构造函数中:
public CalcViewModel()
{
NewFileCommand = new RelayCommand<object>(NewFile);
}
public void NewFile(object sender)
{
//Determine the type of file, based on the ListBoxItem's DataContext.
That is, supposing the ListBoxItem is the object being passed as the sender.
}
基本上,我想要一个 ContextMenu 绑定到不同的 ListBox 组件,这应该会在右键单击时弹出,例如,当在 ContextMenu 上选择新项目时,我想确定文件的类型绑定到列表框。 例如:ListBox 1 绑定到文件类型 B 的集合。ListBox 2 绑定到文件类型 C 的集合。当我右键单击 ListBox 2 中的一个项目并选择新建时,我需要创建一个 C 类型的新文件.
问题 #3
这不是一个非常复杂的视图。我没有使用过 MVVM 框架,因为到目前为止,我认为花时间学习一个框架是值得的,但考虑到这种情况,以及双击 ListBoxItems 的更简单的情况在其中一个代码块中看到,您会推荐使用框架吗?
【问题讨论】:
标签: wpf xaml mvvm listbox contextmenu