Sui*_*eep 1 c# wpf xaml button
假设我有两个按钮定义如下:
<Button Content="ButtonA" Command="{Binding [SomeTerminal].SomeCommand}"/>
<Button Content="ButtonB" Command="{Binding [SomeTerminal].SomeCommand}"/>
Run Code Online (Sandbox Code Playgroud)
我可以知道是否可以获取按钮的内容?用户单击第一个按钮时的含义,我可以ButtonA使用我的SomeCommand方法吗?
您可以使用命令参数:
<Button Content="ButtonA" Command="{Binding [SomeTerminal].SomeCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}" />
<Button Content="ButtonB" Command="{Binding [SomeTerminal].SomeCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}" />
Run Code Online (Sandbox Code Playgroud)
在"纯粹的"MVVM解决方案中,您需要将数据放入ViewModel并将按钮的内容绑定到此数据以显示它.然后,您还可以通过Command参数将绑定数据传递给命令.
<Button Content="{Binding SomeDataA}"
Command="{Binding [SomeTerminal].SomeCommand}"
CommandParameter="{Binding SomeDataA}" />
<Button Content="{Binding SomeDataB}"
Command="{Binding [SomeTerminal].SomeCommand}"
CommandParameter="{Binding SomeDataB}" />
Run Code Online (Sandbox Code Playgroud)
从View获取UI数据被认为是不好的做法,因为它在View上的ViewModel中创建了一个依赖项,使得测试更加困难.
| 归档时间: |
|
| 查看次数: |
2128 次 |
| 最近记录: |