我是 WPF 新手。和其他许多人一样,我试图将 a 绑定ContextMenu到 anObservableCollection以创建动态上下文菜单。Command除了将属性绑定到代表菜单项的类TheCommand的属性之外,一切正常。MenuItemViewModel该命令不会被触发。我究竟做错了什么?
从头开始,ContextMenu是 的子级Image,当鼠标悬停在 上时会显示Image。
<Image.ContextMenu >
<ContextMenu ItemsSource="{DynamicResource ContextMenu}"
Run Code Online (Sandbox Code Playgroud)
其中空的 ContextMenu 定义如下:
<Window.Resources>
<local:MenuItemViewModelCollection x:Key="ContextMenu">
</local:MenuItemViewModelCollection>
<HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}"
ItemsSource="{Binding Path=Children}">
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command"
Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}},
Path=DataContext.TheCommand}"/>
<!-- Value="{Binding Path=TheCommand}" /> I tried this too -->
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
该TheCommand属性定义如下:
<Image.ContextMenu >
<ContextMenu ItemsSource="{DynamicResource ContextMenu}"
Run Code Online (Sandbox Code Playgroud) 我有 TreeView,其中包含不同的项目类型。项目样式是通过自定义 ItemContainerStyleSelector 属性定义的。
我的样式都共享一个基本样式,并且每种样式中只定义了特定于项目的内容。它看起来像这样:
<Style x:Key="BaseStyle" TargetType="{x:Type TreeViewItem}">
...
</Style>
<Style x:Key ="SomeSpecificStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource BaseStyle}">
<Setter Property="ContextMenu" Value="{StaticResource NodeContextMenu}"/>
...
</Style>
<Style x:Key ="SomeSpecificStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource BaseStyle}">
<Setter Property="ContextMenu" Value="{StaticResource AnotherNodeContextMenu}"/>
...
</Style>
Run Code Online (Sandbox Code Playgroud)
上下文菜单定义如下
<ContextMenu x:Key="NodeContextMenu">
<MenuItem Header="Select Views" Command="{Binding Path=OpenViewsCommand}" />
...other specific entries
<MenuItem Header="Remove" Command="{Binding Path=DocumentRemoveCommand}" />
...other entries common for all menus
</ContextMenu>
Run Code Online (Sandbox Code Playgroud)
另一个上下文菜单还应该包含那些常见的项目,例如删除。每次命令属性等发生更改时,都需要通过复制粘贴来复制这些内容。可维护性简直就是地狱。有没有办法定义一个包含常用项目的上下文菜单,然后“派生”特定的上下文菜单?
编辑:我找到了一个带有此线程提示的解决方案:我定义一个包含常见项目的集合,并在定义菜单时使用复合集合以包含新项目和常见项目集合
<CompositeCollection x:Key="CommonItems">
<MenuItem Header="Remove" Command="{Binding Path=DocumentRemoveCommand}">
....Other common stuff
</CompositeCollection>
<ContextMenu x:Key="NodeContextMenu">
<ContextMenu.ItemsSource>
<CompositeCollection>
<MenuItem Header="Select Views" …Run Code Online (Sandbox Code Playgroud) 我有一个 WPF RichTextBox,我想向默认上下文菜单添加更多选项。我不想失去默认的菜单选项(剪切、复制、粘贴)。你能帮我吗?
谢谢
我在基于 Canvas 的 WPF 中制作了一个非常漂亮的 NodeGraph,现在我正在通过右键单击菜单添加一些漂亮的功能。
这些菜单是上下文相关的。这意味着右键单击图形的背景将显示图形 ContextMenu,而右键单击图形中的节点将显示节点 ContextMenu,或者如果该节点有任何带菜单的子控件,则显示这些。
我已经推迟这个特定问题一段时间了,但不能再忽视它了。如标题所述:当我右键单击节点时,将显示图形的上下文菜单。如果我禁用图表的上下文菜单,节点就会正常显示。
这让我相信这是父控件优先打开上下文菜单的问题。我已经尝试过重写父控件和子控件中的鼠标事件并将它们设置为处理,但我就是不明白!
谁能帮助我摆脱这个非常烦人的问题?
我正在 SwiftUI 中开发 Mastodon 客户端,我希望允许用户从上下文菜单中查看帖子的预览,就像许多其他应用程序一样。
我知道我可以在 UIKit 中实现这一点,但我还没有看到如何在 SwiftUI 中实现。
例如,假设我有两个视图:CompactView 和 ExpandedView。CompactView 由Image和组成Text,行数限制为 2,因此每隔一行都被隐藏。ExpandedView 也是由 anImage和 a组成Text,但文本的行数限制为 20,以便显示所有帖子内容。
如果用户用力触摸 CompactView,我想在上下文菜单中显示 ExpandedView 而不是 CompactView。我怎样才能实现这个目标?
struct ContentView: some View {
var body: some View {
List(0 ..< 5) {
CompactView()
}
}
}
struct CompactView: some View {
var body: some View {
HStack {
Image("profile")
Text(/*Post content goes here*/)
.lineLimit(2)
}
.contextMenu {
Button("Report", action: {})
}
}
}
struct ExpandedView: some …Run Code Online (Sandbox Code Playgroud) 我在某些设备上遇到以下情况:当使用点击字段并期望响应时,设备会在屏幕底部中央显示上下文菜单,而不是正确响应该点击事件.
navigationUnclick和trackwheelUnclick
从我读过的内容中,我可以覆盖navigationUnclick和trackwheelUnclick,以防止菜单显示.我这样做是为了屏幕级别,但重现中心菜单场景很困难.这是正确的approch吗?
为什么会这样?有什么方法可以解决这个问题吗?
我在这里有一个示例C#窗体.我需要在鼠标左键单击时显示通知图标的上下文菜单.我已经在下面标记了所需代码的编写位置:
private void button1_Click(object sender, EventArgs e)
{
//Need to show the context menu here
}
Run Code Online (Sandbox Code Playgroud)
请帮忙!
在我的WPF应用程序中,我想将一个上下文菜单及其处理程序添加ViewModel到我的TreeView控件的叶节点中.这是我添加TreeView控件的方式.
<TreeView Name="treePads" ItemsSource="{Binding pads}" Width="190">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:Pad}" ItemsSource="{Binding Members}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" />
<TextBlock Text=" [" Foreground="Blue" />
<TextBlock Text="{Binding Members.Count}" Foreground="Blue" />
<TextBlock Text="]" Foreground="Blue" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:PadInfo}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="["></TextBlock>
<TextBlock Text="{Binding SlotID}" />
<TextBlock Text="] ["></TextBlock>
<TextBlock Text="{Binding WellID}" />
<TextBlock Text="]"></TextBlock>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
Run Code Online (Sandbox Code Playgroud)
以下是此代码的输出结果:

我想添加一个ContextMenu有两个选项:Rename,Delete并添加事件处理程序ViewModel.我怎样才能做到这一点?
我有一个简单的上下文菜单.我想添加一个标题项,它不能被选中,甚至没有用鼠标光标突出显示.当我设置时Enabled = false;我仍然可以标记它并且感觉很愚蠢因为它显然是禁用的并且文本是灰色的.
像这样.我无法标记或选择"菜单"项.它绝不能是蓝色的.所以我想在我的C#应用程序中使用它.简单,没有风格.

public Form1()
{
ContextMenuStrip = new ContextMenuStrip();
ContextMenuStrip.Font = new Font("Arial", 8);
ToolStripItem a = ContextMenuStrip.Items.Add("--- Title ---");
a.Enabled = false;
a.Font = new Font("Consolas", 16, FontStyle.Bold | FontStyle.Italic);
ContextMenuStrip.Items.Add("Alice");
ContextMenuStrip.Items.Add("Bob");
ContextMenuStrip.Items.Add("Conrad");
}
Run Code Online (Sandbox Code Playgroud) 右键单击TextField时,有"撤消","重做","剪切","复制","粘贴","删除"和"全选"选项.
我想从我的控制器类添加一个"Register"MenuItem到该列表,但不知道如何.
这是我到目前为止所得到的:
这会覆盖现有的菜单项:
ContextMenu contextMenu = new ContextMenu();
MenuItem register = new MenuItem("Register");
contextMenu.getItems().add(register);
charName.setContextMenu(contextMenu);
Run Code Online (Sandbox Code Playgroud)
这两个都返回null:
charName.getContextMenu()
charName.contextMenuProperty().getValue()
Run Code Online (Sandbox Code Playgroud) contextmenu ×10
wpf ×5
c# ×4
xaml ×3
winforms ×2
beta ×1
blackberry ×1
command ×1
controls ×1
events ×1
ios ×1
java ×1
javafx ×1
menu ×1
mvvm ×1
notifyicon ×1
richtextbox ×1
swift ×1
swiftui ×1
textfield ×1
treeview ×1
wpf-controls ×1