WPF中Command绑定的默认参数是什么?

时间:2017-03-14 13:30:05

标签: 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为null”。

1 个答案:

答案 0 :(得分:3)

如果您想使用参数,则必须在xaml中添加CommandParameter="..."。但是如果你只是想在不使用参数的情况下使用命令,则不必如此;它们是可选的。