c# - WPF 中命令绑定(bind)的默认参数是什么?

标签 c# wpf xaml data-binding

我有以下 XAML 代码:

<Window x:Class="MVVM.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVM"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="abc" Command="{Binding Path=MyCommand}" Grid.Row="1" />
    </Grid>
</Window>

MyCommand是一个ICommand属性。该命令被调用时没有任何问题,但参数始终为 null

这是默认设置还是我在设置 DataContext 时做错了什么?
如果我做错了什么,应该提出什么论据?

  • 我知道调试器的参数为 null,因为我的命令代码是 (p) => { System.Windows.MessageBox.Show(p?.ToString() ?? "p is null") ; },它总是显示一个带有“p is null”的消息框。

最佳答案

如果您想使用参数,则必须在 xaml 中添加 CommandParameter="..."。但如果您只想像您一样使用不带参数的命令,则不必这样做;它们是可选的。

关于c# - WPF 中命令绑定(bind)的默认参数是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42787377/

相关文章:

c# - 如何在 XAML 中绑定(bind)到静态属性?

c# - MVVM ItemsControl 简单绑定(bind)

c# - 如何在 FiddlerCore 中启用 HTTPS 监控而不解密 SSL

c# - FluentValidation.NET 等同于 [Display(Name)]

c# - Unity Terrain 游戏对象与自定义地形

c# - MVVM WPF C# 自动属性组合框

c# - Process.GetProcesses() 并按内存使用情况排序

wpf - WPF中的圆角扁平按钮

wpf - 条件数据模板

c# - 如何更改 TextBlock 内容绑定(bind)部分的字体?