目前我的代码如下所示:
<Button Command="{Binding DrinkCommand}" CommandParameter="Capuccino" Style="{StaticResource DrinkButton}">
<TextBlock>Capuccino</TextBlock>
</Button>
<Button Command="{Binding DrinkWithSugarCommand}" CommandParameter="Capuccino" Style="{StaticResource DrinkButton}">
<TextBlock>Capuccino + sugar</TextBlock>
</Button>
Run Code Online (Sandbox Code Playgroud)
你可以看到我RelayCommand对加糖和不加糖的卡布奇诺有不同的选择。
我想添加添加额外牛奶的选项。但是我会得到:
DrinkCommand,
DrinkWithSugarCommand,
DrinkWithMilkCommand,
DrinkWithSugarAndMilkCommand。
有没有办法让他们知道DrinkCommand我想要加糖和/或牛奶的饮料(卡布奇诺)?
您可以创建一个类来保存不同的命令参数并按如下方式使用它:
<Button Content="Capuccino + sugar" Command="{Binding DrinkCommand}">
<Button.CommandParameter>
<wpfapp1:MyCommandParameters Milk="false" Sugar="true"/>
</Button.CommandParameter>
</Button>
Run Code Online (Sandbox Code Playgroud)
你的课程看起来像
public class MyCommandParameters {
public bool Milk { get; set; }
public bool Sugar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
您可以在命令代码中使用它,它将作为参数传递。