c# - WPF从按钮传递按钮内容?

标签 c# wpf xaml button

假设我有两个按钮定义如下:

<Button Content="ButtonA" Command="{Binding [SomeTerminal].SomeCommand}"/>
<Button Content="ButtonB" Command="{Binding [SomeTerminal].SomeCommand}"/>

我可以知道是否可以抓取按钮的内容?这意味着当用户单击第一个按钮时,我可以得到 ButtonA在我的SomeCommand方法?

最佳答案

在“纯”MVVM 解决方案中,您需要将数据放入 ViewModel 并将按钮的内容绑定(bind)到该数据以显示它。然后,您还可以通过 Command 参数将绑定(bind)数据传递给命令。

<Button Content="{Binding SomeDataA}" 
        Command="{Binding [SomeTerminal].SomeCommand}" 
        CommandParameter="{Binding SomeDataA}" />
<Button Content="{Binding SomeDataB}" 
        Command="{Binding [SomeTerminal].SomeCommand}" 
        CommandParameter="{Binding SomeDataB}" />

从 View 获取 UI 数据被认为是不好的做法,因为它会在 ViewModel 中创建对 View 的依赖关系,从而使其更难测试。

关于c# - WPF从按钮传递按钮内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22243736/

相关文章:

wpf - 使用 Protractor 对 WPF 中 CefSharp 浏览器下托管的 AngularJS 应用程序进行自动化测试

xaml - 最后一个列表框项目隐藏在 windows phone 8.1 RT 中的命令栏后面

xaml - Windows 10 - 视觉状态更改的转换

windows - 在 WinRT 应用程序中检测击键

c# - ASP.NET session 与 Cookie 过期

.net - 哪个 WPF 控制套件最适合 MVVM?

c# - 如何继承样式并覆盖某些内容?

c# - Kendo Treeview远程绑定(bind)模板内的c#变量

c# - 列表在两个操作之间从 1 计数到 0,无需任何代码即可执行此操作。 (asp.net)

c# - 单击按钮打开新标签页?