WPF:如何使上下文菜单选择并忘记?
我有一个上下文菜单,里面有一个列表框。每当我右键单击控件并从上下文菜单中选择一个值时,最后选择的值都会保持标记状态,并且我无法再次选择该值。
我的想法是,我可以在 COntext 菜单中选择相同的值来打开或关闭属性。
应该是很基本的,我错过了什么?
非常感谢,
编辑:感谢您的回复。我尝试应用你的想法但没有成功。我认为主要问题是上下文菜单的 MenuItems 没有 ItemSource 绑定到集合(例如本例中的 MaybeValues )。
我可以插入我的代码以进行澄清吗:
<Border.ContextMenu>
<ContextMenu>
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property ="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<ContentPresenter x:name="Header" ContentSource="Header" RecognizesAccessKey="True"/>
</ControlTemplate>
</Setter.value>
</Setter>
</Style>
<ContextMenu.ItemContainerStyle>
<ListBox BorderThickness="0" Width="35" Margin="0"
SelectedItem="{Binding Path=Volume, Mode=TwoWay}
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}"
ItemsSource="{Binding Path=PossibleValues}"/>
</ContextMenu>
</Border.ContextMenu>
I have a Contextmenu with a ListBox inside. Whenever I right click my control and select a value from my contextmenu the last selected value stays marked and I can't select that value again.
The idea is, that I may select the same value within the COntextmenu in order to turn a property on or off.
Should be quite basic, what am I missing?
Many Thanks,
EDIT: Thanks for the responses. I have tried to apply your ideas without success. I think the major problem is that MenuItems of a contextmenu have no ItemSource to be bound to a collection (e.g. PossibleValues as in this example).
May I insert my code for clarification:
<Border.ContextMenu>
<ContextMenu>
<ContextMenu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property ="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<ContentPresenter x:name="Header" ContentSource="Header" RecognizesAccessKey="True"/>
</ControlTemplate>
</Setter.value>
</Setter>
</Style>
<ContextMenu.ItemContainerStyle>
<ListBox BorderThickness="0" Width="35" Margin="0"
SelectedItem="{Binding Path=Volume, Mode=TwoWay}
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}"
ItemsSource="{Binding Path=PossibleValues}"/>
</ContextMenu>
</Border.ContextMenu>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么上下文菜单中有一个列表框?列表框允许您从列表中选择一项(或多项,如果启用的话)。一个示例是资源管理器中的文件列表。
ContextMenu 是一个 ItemsControl,即。您可以向其中添加任意数量的项目。所以直接添加项目即可。
编辑:ContextMenu 和 MenuItem 都是 ItemsControl。它们都有一个 ItemsSource 属性。因此,您可以拥有以下内容:
PossibleValues 可以是 MenuItems 的集合。每个菜单项都可以查看以下内容(此处为代码),例如:
EDIT2:尝试如下所示。作为命令,您可以使用 MVVM 帮助程序库中的众多实现之一,例如 DelegateCommand<>来自 Prism 或来自 轻松。
至于命令的执行处理程序,大致如下:
Why do you have a ListBox in your ContextMenu? A ListBox allows you to select one item out of a list (or multiple items, if enabled). An example is the list of files in Explorer.
A ContextMenu is an ItemsControl, ie. you can add an arbitrary number of items to it. So just add the items directly.
EDIT: Both the ContextMenu and MenuItem are ItemsControl. They both have an ItemsSource property. So you could have the following:
PossibleValues could be a collection of MenuItems. Each menuitem could look at follows (here in code), for example:
EDIT2: Try something like the following. As command you could use one of the many implementations from MVVM helper libraries, such as DelegateCommand<> from Prism or SimpleCommand from Cinch.
As for your execute handler of the command, something along the lines of:
这是 ListBox 控件的预期行为 - 它位于 ContextMenu 中并不重要。 ListBox 的默认行为不允许取消选择项目;您可能想要的是常规的可检查菜单项,或者启用了复杂选择模式的列表框。
This is the expected behaviour of a ListBox control - it doesn't matter that it's in a ContextMenu. A ListBox's default behaviour doesn't allow de-selection of items; what you probably want is either a regular chackable MenuItem, or a ListBox with complex selection modes enabled.