我对 ICommand 和 RelayCommand 有点困惑
如果我在自动完成控件上执行此操作
public RelayCommand<KeyEventArgs> AutoCompleteCommand
{
get;
private set;
}
public MyConstructor()
{
AutoCompleteCommand = new RelayCommand<KeyEventArgs>((e) =>
{
//Check if the key pressed is <Enter>
//if it is, check also if the SearchPropertyValue is not String.Empty then
var d = e;
//Should it return true or false?
});
}
在 Xaml 中:
<toolkit:AutoCompleteBox x:Name="acbStore" Margin="154,196,29,0" VerticalAlignment="Top" RenderTransformOrigin="0.6,0.083" Height="162"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding AutoCompleteCommand, Mode=OneWay}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
没发生什么事。
如果我这样做
public ICommand AutoComplete
{
get
{
return new RelayCommand<KeyEventArgs>(e =>
{
var key = e.Key;
});
}
}
在 Xaml 中:
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding AutoComplete, Mode=OneWay}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<toolkit:AutoCompleteBox x:Name="acbStore" Margin="154,196,29,0"
VerticalAlignment="Top" RenderTransformOrigin="0.6,0.083" Height="162"/>
它起作用了,我的命令被触发了。
同样从我看到的所有示例中, RelayCommand 似乎总是在构造函数中。我可以将它粘贴在其他任何地方,因为它会使构造函数非常混乱。