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

2024-03-14

您好,我想从 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 file

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将多个枚举值作为命令参数传递。这可能看起来像这样

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

然后,在您的 ViewModel 中,您可以按如下方式访问枚举

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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐