wpf - 在 WPF 中使用 DataContext 作为 CommandParameter

标签 wpf xaml binding datacontext

我想将当前 DataContext(它是 ViewModel 的一个实例)作为 WPF 按钮上的 CommandParameter 传递。我应该使用什么语法?

<Button 
  x:Name="btnMain"
  Command="infra:ApplicationCommands.MyCommand"
  CommandParameter="{Binding ???}"
 />

最佳答案

一个空的 Binding,没有路径,直接绑定(bind)到 DataContext,所以

{Binding}

足以让它工作!你的例子:
<Button 
  x:Name="btnMain"
  Command="infra:ApplicationCommands.MyCommand"
  CommandParameter="{Binding}"
 />

关于wpf - 在 WPF 中使用 DataContext 作为 CommandParameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2319752/

相关文章:

.net - XAML -(键入)它有什么意义?

c# - WPF App 变成 GeneratedApplication

java - 如何将 TableView 内的 TableColumn 的总和绑定(bind)到外部 Label textProperty()?

wpf - mvvm 更新计算字段

c# - 为什么这个随机数生成器不是随机的?

wpf - 带有静态标题的 ItemsControl

c# - UWP XAML 弹出窗口不尊重垂直对齐?

wpf - 从用户控件绑定(bind)到父用户控件的属性

c# - TreeView 的子节点无法反射(reflect)绑定(bind)的变化

wpf - 如何在 View 模型中检索 Canvas 尺寸