我有以下 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/