如何在 Xaml 的“命令参数”中传递两个枚举值,并在 CS 文件中获取这些值

本文关键字:枚举 两个 并在 获取 文件 CS Xaml 命令 参数 命令参数 | 更新日期: 2023-09-27 18:34:08

嗨,我想从 Xaml 文件传递两个枚举值,并希望获取这些值。我创建了一个具有两个值变量并使用该类型的类。但我不知道如何通过 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}">

如何在 Xaml 的“命令参数”中传递两个枚举值,并在 CS 文件中获取这些值

你不一定需要那个XModelProperty类。相反,您可以使用 MultiBinding 将多个枚举值作为命令参数传递。这可能看起来像这样

在 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 中的资源:

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();
  }
}

然后,在您的视图模型中,您可以按如下方式访问您的枚举

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 中创建 XModelProperty 的对象,并将其作为 CommandParameter 传递。如下:

 <Button.......
 <Button.CommandParameter>
     <local:XModelProperty bMode="A" dStyle="C" />
 </Button.CommandParameter>

在视图模型中,您将直接获得对象,而无需进行任何转换:

 Command = new RelayCommand(param => this.CommandAction(param));
    }

    public RelayCommand Command { get; set; }
    private void CommandAction(object param)
    {
        var xModelProperty = param as XModelProperty;
    }

定义转换器:

<local:NotBoolToVisibilityConverter x:Key="NotBoolToVisibilityConverter" />

{Binding Converter={StaticResource NotBoolToVisibilityConverter}}

1) 创建转换器

public class MultiInOneConverter : 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();
      }
}

2) 创建枚举

public enum StyleModes
{
      Somex,
      Somey,
      Somez
}

3) 在资源中定义转换器

<Window.Resources>
    <local:MultiInOneConverter x:Key="MultiInOneConverter"/>
</Window.Resources>

4)全部使用

<Button Content="WithConverter" Command="{Binding WithConverterCommand}">
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource MultiInOneConverter}">
                    <Binding>
                        <Binding.Source>
                            <local:StyleModes>Somex</local:StyleModes>
                        </Binding.Source>
                    </Binding>
                    <Binding>
                        <Binding.Source>
                            <local:StyleModes>Somey</local:StyleModes>
                        </Binding.Source>
                    </Binding>
                </MultiBinding>
            </Button.CommandParameter>
</Button>