如何在“命令参数”中传递两个枚举值。在Xaml中并在CS文件中获取这些值 [英] How to pass two enums values in a "command parameter" in Xaml and get those values in CS file

查看:56
本文介绍了如何在“命令参数”中传递两个枚举值。在Xaml中并在CS文件中获取这些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Xaml文件传递两个枚举值,并想要获取这些值。我制作了一个具有两个值变量并使用该类型的类。但是我不知道如何通过Xaml传递该类对象。

Hi I want to pass two enum values from Xaml file and want to get those values. I made a class having two value variables and using that type. but I dont know how to pass that class object through Xaml.

Xaml文件

<dxb:BarButtonItem x:Name="PointsItem" Content="Points" RibbonStyle="Large"  Command="{Binding DrawStyleItemCommand}" >
            <dxb:BarButtonItem.CommandParameter>
              <MultiBinding Converter="{StaticResource xmlns:convertor.ModelPropertyConverter}">
                <Binding Source="{x:Static enums:ModelModes.somex}" />
                <Binding Source="{x:Static enums:StyleModes.somey}" />
              </MultiBinding>
            </dxb:BarButtonItem.CommandParameter>
          </dxb:BarButtonItem>

cs文件

private DelegateCommand<object> _drawStyleItemCommand;
        public DelegateCommand<object> DrawStyleItemCommand
        {
            get { return _drawStyleItemCommand ?? (_drawStyleItemCommand = new DelegateCommand<object>(StyleItem)); }
        }
     private void StyleItem(object parameter)
        {
            var values = (object[])parameter;
            var enum1 = (ModelModes)values[0];
            var enum2 = (DrawStyleModes)values[1];
        }
XModel Class

     public class XModelProperty
        {
            public XModelModes bMode { get; set; }
            public XStyleModes dStyle { get; set; }

            public XModelProperty(BoneModelModes _bMode,DrawStyleModes _dStyle)
            {
                bMode = _bMode;
                dStyle = _dStyle;

            }        
        }

转换器类别

namespace Infra.Converter
{
    public class BoneModelPropertyConverter : IMultiValueConverter
    {

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return values.Clone();
        }

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

我在解析异常这行< MultiBinding Converter = {StaticResource xmlns:convertor.BoneModelPropertyConverter}>

推荐答案

您不一定需要该 XModelProperty 类。而是使用 MultiBinding 传递多个枚举值作为命令参数。这可能看起来像这样

You don't necessarily need that XModelProperty class. Instead you use MultiBinding to pass multiple enum values as command parameter. This could look something like this

在您的XAML中:

<Button Content="Click me" Command="{Binding DrayStyleItemCommand }">
  <Button.CommandParameter>
    <MultiBinding Converter="{StaticResource MultiValueConverter}">
      <Binding Source="{x:Static enums:StyleModes.Somex}" />
      <Binding Source="{x:Static enums:StyleModes.Somey}" />
    </MultiBinding>
  </Button.CommandParameter>
</Button>

创建 MultiValueConverter 并将其添加为资源在您的XAML中:

Create a MultiValueConverter and add it as resource in your XAML:

public class MultiValueConverter : IMultiValueConverter
{
  public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
  {
    return values.Clone();
  }

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

然后,在ViewModel中,您可以访问枚举

And then, in your ViewModel you can access your enums as follows

private DelegateCommand<object> _drawStyleItemCommand;
public DelegateCommand<object> DrawStyleItemCommand
{
  get { return _drawStyleItemCommand ?? (_drawStyleItemCommand = new DelegateCommand<object>(StyleItem)); }
}

private void StyleItem(object parameter)
{
  var values = (object[])parameter;
  var enum1 = (StyleModes)values[0];
  var enum2 = (StyleModes)values[1];
}

这篇关于如何在“命令参数”中传递两个枚举值。在Xaml中并在CS文件中获取这些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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