WPF:如何使上下文菜单选择并忘记?

发布于 2024-08-30 12:48:57 字数 1262 浏览 1 评论 0原文

我有一个上下文菜单,里面有一个列表框。每当我右键单击控件并从上下文菜单中选择一个值时,最后选择的值都会保持标记状态,并且我无法再次选择该值。

我的想法是,我可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海夜未眠 2024-09-06 12:48:57

为什么上下文菜单中有一个列表框?列表框允许您从列表中选择一项(或多项,如果启用的话)。一个示例是资源管理器中的文件列表。

ContextMenu 是一个 ItemsControl,即。您可以向其中添加任意数量的项目。所以直接添加项目即可。

编辑:ContextMenu 和 MenuItem 都是 ItemsControl。它们都有一个 ItemsSource 属性。因此,您可以拥有以下内容:

<ContextMenu
    DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}"
    ItemsSource="{Binding Path=PossibleValues}"/>

PossibleValues 可以是 MenuItems 的集合。每个菜单项都可以查看以下内容(此处为代码),例如:

var menuItem = new MenuItem();
menuItem.Items.Add(new TextBlock { Text = "Something" });
menuItem.Command = DoSomethingCommand;
menuItem.CommandParameter = "identifier";

EDIT2:尝试如下所示。作为命令,您可以使用 MVVM 帮助程序库中的众多实现之一,例如 DelegateCommand<>来自 Prism 或来自 轻松

PossibleValues = new ObservableCollection<MenuItem>();

// null value
var nullMenuItem = new MenuItem();
nullMenuItem.Items.Add(new TextBlock { Text = "Null" });
nullMenuItem.Command = DoSomethingCommand;
nullMenuItem.CommandParameter = null;
PossibleValues.Add(nullMenuItem);

// Values one to nine
for (int i = 1; i < 9; i++)
{
    var menuItem = new MenuItem();
    menuItem.Items.Add(new TextBlock { Text = i.ToString() });
    menuItem.Command = DoSomethingCommand;
    menuItem.CommandParameter = i;
    PossibleValues.Add(menuItem);
}

至于命令的执行处理程序,大致如下:

public void DoSomethingCommand_Execute(object commandParameter)
{
    this.SelectedNumber = commandParameter as int?;
    // Or whatever you actually want to do in response to the selection.
}

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:

<ContextMenu
    DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}"
    ItemsSource="{Binding Path=PossibleValues}"/>

PossibleValues could be a collection of MenuItems. Each menuitem could look at follows (here in code), for example:

var menuItem = new MenuItem();
menuItem.Items.Add(new TextBlock { Text = "Something" });
menuItem.Command = DoSomethingCommand;
menuItem.CommandParameter = "identifier";

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.

PossibleValues = new ObservableCollection<MenuItem>();

// null value
var nullMenuItem = new MenuItem();
nullMenuItem.Items.Add(new TextBlock { Text = "Null" });
nullMenuItem.Command = DoSomethingCommand;
nullMenuItem.CommandParameter = null;
PossibleValues.Add(nullMenuItem);

// Values one to nine
for (int i = 1; i < 9; i++)
{
    var menuItem = new MenuItem();
    menuItem.Items.Add(new TextBlock { Text = i.ToString() });
    menuItem.Command = DoSomethingCommand;
    menuItem.CommandParameter = i;
    PossibleValues.Add(menuItem);
}

As for your execute handler of the command, something along the lines of:

public void DoSomethingCommand_Execute(object commandParameter)
{
    this.SelectedNumber = commandParameter as int?;
    // Or whatever you actually want to do in response to the selection.
}
帅的被狗咬 2024-09-06 12:48:57

这是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文