“冒泡”是指绑定viewmodel的事件转到父级 [英] "Bubbling" events from bound viewmodel goes to parent

查看:88
本文介绍了“冒泡”是指绑定viewmodel的事件转到父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我仍然习惯于Caliburn和WPF,所以我做错了方法。我想知道更好的方法!

First off, I'm still getting used to Caliburn and WPF, so I may be doing this the wrong way. I'd love to know a better way!

我有一个 Conductor 壳视图模型,该模型具有一个视图带有一些共享元素,以及 ActiveItem 的内容控件。现在,我希望将 ActiveItem 视图模型中的一组按钮显示在内容控件之外,但仍将其单击事件转到活动视图模型中。我试过像这样添加 ItemsControl

I have a Conductor shell view model, which has a view with some shared elements, and a content control for the ActiveItem. Now, I want a set of buttons from the ActiveItem view model to be displayed outside the content control, but still have their click events go to the active view model. I've tried adding an ItemsControl like this:

    <ItemsControl x:Name="ActiveItem_Buttons">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel DockPanel.Dock="Right" Orientation="Horizontal" HorizontalAlignment="Right"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Content="{Binding Name}" Margin="10" Height="25" Width="80">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="Click">
                            <cal:ActionMessage MethodName="{Binding Name}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

(我正在执行 Interaction.Trigger 部分,因为我无法绑定到 x:Name ,这将导致Caliburn自动对其进行连线)

(I'm doing the Interaction.Trigger part since I cannot bind to x:Name which would cause Caliburn to wire it automagically)

但是,我得到一个异常,指出Caliburn无法找到匹配的方法。似乎它开始从外壳视图模型而不是活动项目视图模型冒泡事件。如何设置事件冒泡的起点?我找到并尝试了附加属性 cal:View.Model = {Binding ActiveItem} ,但这似乎无济于事。

However, I get an exception stating that Caliburn cannot find a matching method. It seems like it starts bubbling the event from the shell view model, instead of the active item view model. How can I set the "starting point" of the event bubbling? I found and tried the attached property cal:View.Model="{Binding ActiveItem}", but that did not seem to help.

编辑:为清楚起见: Buttons 属性是在视图模型基类 ButtonScreenBase <上定义的/ code>,并且外壳程序会继承 Conductor< ButtonScreenBase>

For clarity: the Buttons property is defined on the view model base class ButtonScreenBase, and the shell inherits Conductor<ButtonScreenBase>

推荐答案

更新:回过头来查看有关此特定问题的其他帖子,这是该人本人对此问题的解释。

update: went back to check some other posts about this particular problem, an explanation about the problem from the man himself.

将带有Caliburn.Micro的命令绑定到ListView中的按钮

 <Button Content="{Binding Name}" cal:Bind="{Binding}" Margin="10" Height="25" Width="80">
     <i:Interaction.Triggers>
         <i:EventTrigger EventName="Click">
             <cal:ActionMessage MethodName="{Binding Name}" />
          </i:EventTrigger>
     </i:Interaction.Triggers>
</Button>

同一件事是数据模板化的继承问题。

same thing it's an inherit issue with datatemplating.

这篇关于“冒泡”是指绑定viewmodel的事件转到父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆