c# - 将用户定义的属性直接传递到 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?
解决方案
是的,有可能。但是对于您可以将值传递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>
推荐阅读
- python - Python pandas 遍历特定列
- reactjs - React Fetch 数据并通过 context API 将其保存在全局状态
- vue.js - 为什么更改子组件的属性会影响父组件?
- c# - 如何使用带有正文的 api 并键入 x-www-form-urlencoded?
- c - 为什么冗余间接寻址使程序在 x86 CPU 上运行得更快?
- python - .partitionBy('id') 在 PySpark 中写入 BigQuery 表时
- mysql - 如何选择不同的 *
- vue.js - 为所有实例设置组件属性/将相同的数据传递给所有组件实例
- html - 我在亚马逊 ec2 服务器中找不到我的 www 文件夹
- javascript - Print Json data in html tabel by javascript