Wpf 内部GroupBox不会吞噬交互事件

Wpf 内部GroupBox不会吞噬交互事件,wpf,xaml,Wpf,Xaml,我在父级GroupBox中有一个GroupBox。他们两个都有自己的 <i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonDown"> <i:InvokeCommandAction Command="{Binding ...}" /> </i:EventTrigger> </i:Interaction.Triggers> 当我按下

我在父级
GroupBox
中有一个
GroupBox
。他们两个都有自己的

<i:Interaction.Triggers>
  <i:EventTrigger EventName="MouseLeftButtonDown">
     <i:InvokeCommandAction Command="{Binding ...}"  />
  </i:EventTrigger>
</i:Interaction.Triggers>

当我按下内部
GroupBox
时,它会触发自己的
命令
,然后父
命令也会触发


我如何防止这种情况?如何使内部
GroupBox
吞下事件?

您可以使用另一个TriggerAction实现,该实现支持将事件参数作为命令参数传递给命令,例如库中的
EventToCommand
类:

这种解决方案的丑陋之处在于视图模型依赖于与视图相关的
MouseButtonEventArgs
类型。如果您不喜欢这样,您可以按照@adabyron的建议在此处实施您自己的行为:


然后,您可以直接在行为中设置MouseButtonEventArgs的Handled属性,而不是将其传递给视图模型。

这就是解决方案!如果我的命令看起来像这样,则返回新的RelayCommand(this.OnInnerCommand)
,我如何传递
鼠标按钮ventargs
?另外,我想你是想把
arg=>true
放在第二位,如果您引用的是
CanExecute
。您只能传递一个命令,因此您需要实现自定义行为,或者在视图的代码隐藏中处理内部GroupBox的MouseLeftButtonDown事件,并在将Handled属性设置为true后从中调用视图模型的命令。
<GroupBox Header="Outer" xmlns:mvvm="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonDown">
            <i:InvokeCommandAction Command="{Binding OuterCommand}"  />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock>...</TextBlock>
        <GroupBox Header="Inner" Grid.Row="1">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonDown">
                    <mvvm:EventToCommand Command="{Binding InnerCommand}" PassEventArgsToCommand="True"  />
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <TextBlock>inner...</TextBlock>
        </GroupBox>
    </Grid>
</GroupBox>
public class ViewModel
{
    public ViewModel()
    {

        OuterCommand = new RelayCommand(arg => true, (arg)=> { MessageBox.Show("outer"); });
        InnerCommand = new RelayCommand(arg => true, 
            (arg) => 
            {
                MessageBox.Show("inner");
                MouseButtonEventArgs mbea = arg as MouseButtonEventArgs;
                if (mbea != null)
                    mbea.Handled = true;
            });
    }

    public RelayCommand OuterCommand { get; }

    public RelayCommand InnerCommand { get; }
}