首页 > 解决方案 > 将用户定义的属性直接传递到 WPF 命令中,而不是先将其保存在 ViewModel 中

问题描述

下面你会看到 stackPanel,用户必须使用单选按钮从枚举中选择一个选项。所以,CoffeeStrength 是一个枚举,使用转换器,我可以设置正确的值。

<StackPanel Grid.Column="0" Orientation="Vertical" HorizontalAlignment="Center">
    <Label FontWeight="Bold">Strength</Label>
    <RadioButton GroupName="Strength" IsChecked="{Binding Path=CoffeeStrength, Converter={StaticResource EnumToBool}, ConverterParameter=Weak}">Weak</RadioButton>
    <RadioButton GroupName="Strength" IsChecked="{Binding Path=CoffeeStrength, Converter={StaticResource EnumToBool}, ConverterParameter=Normal}">Normal</RadioButton>
    <RadioButton GroupName="Strength" IsChecked="{Binding Path=CoffeeStrength, Converter={StaticResource EnumToBool}, ConverterParameter=Strong}">Strong</RadioButton>
</StackPanel>

下面你会看到我的按钮,它使用自定义类作为 CommandParameter。我想将上面的值 (CoffeeStrength) 作为附加参数添加到此命令中,而不是将值保存到 ViewModel 中的 CoffeeStrength 中(请参阅绑定)。

<Button Content="Cappuccino + sugar"
        Command="{Binding DrinkCommand}"
        Style="{StaticResource DrinkButton}">
     <Button.CommandParameter>
           local:DrinkCommandParameters Name="Cappuccino" Milk="False" Sugar="True"/>
     </Button.CommandParameter>
</Button> 

换句话说,我想从我的 ViewModel 中删除 CoffeeStrength 属性,并且只将它传递给 DrinkCommand。因为我只需要知道激活命令时的值。下面你看到枚举和不需要的?视图模型中的属性。代码中从不使用 setter,因为用户决定强度。

public enum Strength
    {
        Normal = 0, Weak, Strong
    }

private Strength _coffeeStrength;
    public Strength CoffeeStrength
    {
        get { return _coffeeStrength; }
        set { _coffeeStrength = value; RaisePropertyChanged(() => CoffeeStrength); }
    }

有没有办法从 ViewModel 中删除 CoffeeStrength 属性并将值直接传递给 XAML 中的 DrinkCommand?

标签: c#wpfxaml

解决方案


是的,有可能。但是对于您可以将值传递CoffeeStrength给命令,您必须DrinkCommandParameters使用依赖属性(例如 CoffeeStrength)扩展您的类。我怀疑这是一个更好的解决方案,只需将其留在 ViewModel 中

所以解决方案是:

  • DrinkCommandParameters使用依赖属性扩展您的
  • x:Name为每个设置RadioButton
  • 使用MultiBinding与每个RadioButton例如 via的绑定来ElementName设置咖啡强度的依赖属性。
  • 用这个转换器实现一个类IMultiValueConverter并在MultiBinding

然后您的 XAML 几乎可以这样看:

<StackPanel Grid.Column="0" Orientation="Vertical" HorizontalAlignment="Center">
    <Label FontWeight="Bold">Strength</Label>
    <RadioButton x:Name="rbWeak" GroupName="Strength" IsChecked="True">Weak</RadioButton>
    <RadioButton x:Name="rbNormal" GroupName="Strength" IsChecked="False">Normal</RadioButton>
    <RadioButton x:Name="rbStrong" GroupName="Strength" IsChecked="False">Strong</RadioButton>
</StackPanel>
<Button Content="Cappuccino + sugar" Command="{Binding DrinkCommand}" Style="{StaticResource DrinkButton}">
    <Button.CommandParameter>
        <local:DrinkCommandParameters Name="Cappuccino" Milk="False" Sugar="True">
            <local:DrinkCommandParameters.CofeeStrength>
                <MultiBinding Converter="{StaticResource YourCofeeStrengthMultiValueConverter}">
                    <Binding Path="IsChecked" ElementName="rbWeak"/>
                    <Binding Path="IsChecked" ElementName="rbNormal"/>
                    <Binding Path="IsChecked" ElementName="rbStrong"/>
                </MultiBinding>
            </local:DrinkCommandParameters.CofeeStrength>
        </local:DrinkCommandParameters>
    </Button.CommandParameter>
</Button>

推荐阅读