【发布时间】:2020-11-29 11:13:02
【问题描述】:
目前,我的 calculator 上的每个按钮在 ShellViewmodel 中都有一个方法:
Shellview:
<Button x:Name="Btn1" Content="1"/>
<Button x:Name="Btn2" Content="2"/>
<Button x:Name="Btn3" Content="3"/>
ShellViewmodel:
private string _input = string.Empty;
public string Input
{
get { return _input; }
set
{
_input = value;
NotifyOfPropertyChange(() => Input);
}
}
public void Btn1()
{
Input += "1";
}
public void Btn2()
{
Input += "2";
}
public void Btn3()
{
Input += "3";
}
我意识到这是对我的计算器进行编程的一种幼稚方式,更好的解决方案是让每个按钮调用相同的方法并传递一个值(Btn1 将传递一个值 1,等等)。下面的代码不起作用;当我运行它并单击按钮时,什么也没有发生。因为我是初学者,所以我可能在装订方面犯了非常愚蠢的错误。将多个按钮绑定到一个方法的方法是什么,每个按钮都传递自己的值?谢谢。
Shellview:
<Button x:Name="Btn1" Command="{Binding NumberInput}" CommandParameter="1" Content="1"/>
<Button x:Name="Btn2" Command="{Binding NumberInput}" CommandParameter="2" Content="2"/>
<Button x:Name="Btn3" Command="{Binding NumberInput}" CommandParameter="3" Content="3"/>
ShellViewmodel:
private string _input = string.Empty;
public string Input
{
get { return _input; }
set
{
_display = value;
NotifyOfPropertyChange(() => Display);
}
}
public void NumberInput(string valuePassedFromButton)
{
Input += valuePassedFromButton;
}
【问题讨论】:
标签: c# wpf caliburn.micro