Silverlight 4 事件触发器已处理

发布于 12-06 00:41 字数 1618 浏览 1 评论 0原文

我的应用程序中有两个嵌套的网格(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李白2024-12-13 00:41:52

我们通过添加名为 IsInner 的依赖属性来扩展 EventTrigger,然后我们始终在内部 EventTrigger 中设置静态标志。外部 EventTrigger 取消设置该标志,并在设置该标志时返回。这非常容易编写并且效果很好。

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.

永言不败2024-12-13 00:41:52

最好的选择是将事件参数传递给命令,然后使用事件参数标记处理的事件。您可以按照 这个例子在这里

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文