如何在ViewModel中获取Button的内容?

我有一个命令设置为viewModel的按钮。 运行时填充值的按钮。 任何人都可以帮我在viewModel中获取按钮内容吗?

4个按钮在运行时具有4个不同的值集,并且基于按钮单击我将采取行动。 说出4个按钮第二个按钮是点击然后我将减去100分。 如果单击第4个按钮,我将在scrore中添加40。 这100和40是按钮内容。

任何人都可以帮我在viewModel运行时获取按钮值吗?

谢谢

如果这是WPF – 这样做有帮助吗?

XAML

                   

代码背后:

 namespace WpfCommandTest 

{using System.Windows; 使用System.Windows.Input;

 ///  /// Interaction logic for MainWindow.xaml ///  public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void OnDoSomethingExecuted(object sender, ExecutedRoutedEventArgs e) { //Look at e.Parameter <---!!!This line gives you the value of button content } } 

}

想法是使用命令参数...

对按钮进行命令绑定时,提供命令参数作为按钮的内容

 CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Content}