Silverlight 4 事件触发器已处理
我的应用程序中有两个嵌套的网格(FrameworkElement)项目。
<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>
每个 InvokeCommand 都附加到视图模型中的一个 DelegateCommand (来自 Prism 库)。
OuterCommand = new DelegateCommand(OuterCommandMethod, e => true);
InnerCommand = new DelegateCommand(InnerCommandMethod, e => true);
目前,由于 MouseLeftButtonEvent 未在 InnerGrid< 处处理,InnerGrid 上的 EventTrigger 也会触发 OuterGrid 上的事件。 /em> 级别。
有没有办法可以通知 EventTrigger 它已被处理并且它不应该冒泡到 OuterGrid?
目前,我能想到的就是在 InnerGrid 周围有一个包装器 FrameworkElement,它使用 XAML 代码隐藏上的事件来设置要处理的事件。还有人有其他想法吗?
---- 编辑 ---- 最后,我在我的应用程序中包含了 MVVM Light,并用 RelayCommand 替换了 InvokeCommandAction。现在这正按我的预期进行。我将把科比的回答标记为给我建议的获胜者。
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.
我们通过添加名为
IsInner
的依赖属性来扩展EventTrigger
,然后我们始终在内部EventTrigger
中设置静态标志。外部EventTrigger
取消设置该标志,并在设置该标志时返回。这非常容易编写并且效果很好。We have extended
EventTrigger
by adding dependency property calledIsInner
and then we always set a static flag in the innerEventTrigger
. The outerEventTrigger
unsets the flag and returns if the flag was set. That is extremely easy to write and works well.