lys*_*cid 3 .net c# wpf binding mvvm
我正在构建一个简单的Calculator应用程序.我也在学习如何在我的应用程序中应用MVVM模式.
我希望我的计算器中的每个"数字"按钮都绑定到同一个命令,它们只会在引发命令的按钮的数字(文本)上有所不同.
例如,当单击按钮"1"时,我希望收到有关它的通知,从发件人的属性中提取"1",并继续所需的其余工作.
这允许我定义一个方法而不是10个不同的处理程序.
到目前为止,我在所有MVVM教程中看到的都是不可能的,因为在绑定到将处理点击的实际方法时,Command绑定不会向我提供所有这些信息.
有没有办法轻松做我需要的?
假设我理解你要做的事情,你可以使用CommandParameter属性让不同的按钮为同一个命令提供值.例如:
...
<Button Content="1" Command="{Binding ButtonClickCommand}" CommandParameter="1"/>
<!-- Or, bind directly to the button's content using RelativeSource, like so: -->
<Button Content="2" Command="{Binding ButtonClickCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}"/>
...
Run Code Online (Sandbox Code Playgroud)
并在您的命令的委托方法中:
private void ButtonClickCommandHandler(object parameter)
{
switch(int.Parse(parameter.ToString()))
{
case 1:
...
case 2:
...
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2697 次 |
| 最近记录: |