【发布时间】:2016-04-15 14:14:21
【问题描述】:
我有一个组合框,我需要在我的视图模型中使用一个命令来绑定到它的 ContextMenuOpening 事件。我尝试引用 System.Windows.Interactivity 并使用 InvokeCommandAction,但该命令没有调用。有谁知道我哪里出错了?
<ComboBox x:Name="comboBoxAs" Grid.Column="0" VerticalAlignment="Top" Margin="928,62,0,0" Height="25"
ItemsSource="{Binding Source={StaticResource sas}}"
SelectedItem="{Binding Path=as, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource ComboBoxDefault}" HorizontalAlignment="Left" Width="212" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="ContextMenuOpening">
<i:InvokeCommandAction Command="{Binding ContextMenuOpeningCommand, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
视图模型:
public ICommand ContextMenuOpeningCommand
{
get
{
if (_contextMenuOpeningCommand == null)
{
_contextMenuOpeningCommand = new RelayCommand<object>(param => this.ContextMenuOpening(),
null);
}
return _contextMenuOpeningCommand;
}
}
public void ContextMenuOpening()
{
System.Windows.MessageBox.Show("test", "test");
}
private ICommand _contextMenuOpeningCommand;
【问题讨论】:
-
您尝试过不同的活动吗?也许 DropDownOpened 看看命令是否被击中。我试过了,它在这里工作,唯一的区别是我使用了 DelegateCommand,但这应该没关系。
-
是的!就是这样。非常感谢您的帮助。如果您将此添加为答案,我很乐意将其标记为这样。
标签: wpf xaml mvvm blend icommand