I have two nested Grid (FrameworkElement) items in my application.

<UserControl xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">        
<Grid x:name="OuterGrid">
          <i:Interaction.Triggers>
          <i:EventTrigger EventName="MouseLeftButtonDown">
           <i:InvokeCommandAction x:Name="TheOuterCommand" Command="{Binding OuterCommand}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
            <Grid x:name="InnerGrid">
             <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseLeftButtonDown">
                    <i:InvokeCommandAction x:Name="TheInnerCommand" Command="{Binding InnerCommand}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
        </Grid>
    </Grid>
</UserControl>

Each of the InvokeCommands is attached to a DelegateCommand (from the Prism libraries) in the viewmodel.

OuterCommand = new DelegateCommand(OuterCommandMethod, e => true);
InnerCommand = new DelegateCommand(InnerCommandMethod, e => true);

At the moment, the EventTrigger on InnerGrid also triggers the event on the OuterGrid due to the MouseLeftButtonEvent not being handled at the InnerGrid level.

Is there a way I can notify the EventTrigger that it is handled and it should not bubble up to the OuterGrid?

At the moment, all I can think to do is have a wrapper FrameworkElement around the InnerGrid that uses an event on the XAML code-behind to set the event to handled. Does anyone have any other ideas?

---- Edit ---- In the end, I have included MVVM Light in my application and replaced InvokeCommandAction with RelayCommand. This is now working as I intended. I'll mark Bryant's answer as the winner for giving me the suggestion.

有帮助吗?

解决方案

Your best bet would be to pass the event args to the Command and then mark the event handled using the event args. You can do this by following this example here.

其他提示

We have extended EventTrigger by adding dependency property called IsInner and then we always set a static flag in the inner EventTrigger. The outer EventTrigger unsets the flag and returns if the flag was set. That is extremely easy to write and works well.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top