从枚举值列表创建可检查的上下文菜单 [英] Creating a checkable context menu from a list of enum values

查看:134
本文介绍了从枚举值列表创建可检查的上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多人问我类似的问题,但到目前为止,我无法将这些部分放在一起来解决我的问题。

I see lots of people asking questions similar to mine, but so far I haven't been able to put the pieces together to solve my problem.

我有某个枚举类型(我们称之为 MyCustomEnum ),我想用它来创建一个 ContextMenu 。我想要菜单项可以检查,并且选中 MenuItem 绑定到应用程序使用的静态设置(我们将调用的属性 MyCustomEnumSetting 在设置类 MyCustomSettingsClass )。

I have a certain enum type (let's call it MyCustomEnum) which I'd like to use to create the list of choices in a ContextMenu. I'd like the menu items to be checkable, and have the checked MenuItem bound to a static setting the application uses (a property we'll call MyCustomEnumSetting on the settings class MyCustomSettingsClass).

到目前为止,我可以生成 ContextMenu ,并根据设置值检查正确的 MenuItem 。我使用 MultiBinding 来比较 MenuItem DataContext 中的枚举值, / code>到设置类枚举的值,看看它们的值是否相等。但是,绑定只是一种方法:单击未选中的 MenuItem 不会更新绑定。我有一种感觉我错过了一些东西,但WPF的这一部分对我来说有点模糊。

So far, I'm able to generate the ContextMenu and check the correct MenuItem, based on the setting value. I did this using a MultiBinding to compare the enum value in the DataContext of the MenuItem to the value of the settings class enum and see if their values equal. However, the binding is only one way: clicking on an unchecked MenuItem doesn't update the binding. I have a feeling I'm missing something, but this part of WPF is a bit more fuzzy to me.

这是我到目前为止:

<UserControl>
    <UserControl.Resources>
        <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type system:Enum}" x:Key="MyCustomEnumProvider">
            <ObjectDataProvider.MethodParameters>
                <x:Type Type="local:MyCustomEnum" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <local:EnumEqualsConverter x:Key="EnumEqualsConverter" />
    </FrameworkElement.Resources>
   <FrameworkElement.ContextMenu>
        <ContextMenu ItemsSource="{Binding Source={StaticResource MyCustomEnumProvider}}">
            <ContextMenu.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter
                        Property="IsChecked">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource EnumEqualsConverter}">
                                <MultiBinding.Bindings>
                                    <!--First binding source is the application setting value-->
                                    <Binding Source="{x:Static local:MyCustomSettingsClass.Default}" Path="MyCustomEnumSetting" />
                                    <!--Second binding source is the enum value in the data context of the MenuItem-->
                                    <Binding RelativeSource="{RelativeSource Self}" Path="DataContext" Mode="OneWay" />
                                </MultiBinding.Bindings>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ContextMenu.ItemContainerStyle>
        </ContextMenu>
    </FrameworkElement.ContextMenu>
</FrameworkElement>

而我的 IMultiValueConverter 的代码: / p>

And the code for my IMultiValueConverter:

public sealed class EnumEqualsConverter : IMultiValueConverter
{
    object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        IEnumerable<Enum> enums = values.Cast<Enum>();
        var value1 = enums.ElementAt(0);
        var value2 = enums.ElementAt(1);
        return value1.Equals(value2);
    }

    object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

对于不同方向的任何建议,我可以去获得我正在寻找的结果?

Any suggestions for a different direction I could go to get the results I'm looking for?

推荐答案

你已经使用Multibinding来检查你的复选框..这将是一种方法,除非你在您的MultiValueConverter的ConvertBack方法中做了一些魔术。您想要实现的事情可以像下面那样完成:

You have used Multibinding to check your checkbox.. It will be one way unless you have done some magic in ConvertBack method of your MultiValueConverter. The thing that you want to achieve can be done like below:

通过您的MultiBinding,您可以将您的Menuitem绑定到VM / codebeind上的命令,然后发送Menuitem的DataContext在CommandParameter中。在您的commmand处理程序中,您可以使用命令参数更新静态属性。

With your MultiBinding in place, you can bind your Menuitem to a command on your VM/codebeind and send the Menuitem's DataContext in CommandParameter. In your commmand handler you can update you static property with the command parameter.

     <Style TargetType="MenuItem">
                    <Setter Property="Command" Value="{Binding MyCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
                    <Setter Property="CommandParameter" Value="{Binding}"/>
      </Style>

这篇关于从枚举值列表创建可检查的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆