我有以下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
时我做错了什么?
如果我做错了什么,论证应该是什么?
(p) => { System.Windows.MessageBox.Show(p?.ToString() ?? "p is null"); }
,它总是显示一个消息框,其中“p为null”。答案 0 :(得分:3)
如果您想使用参数,则必须在xaml中添加CommandParameter="..."
。但是如果你只是想在不使用参数的情况下使用命令,则不必如此;它们是可选的。