Caliburn Micro和ContextMenu导致找不到目标

时间:2015-01-31 20:14:04

标签: c# wpf contextmenu caliburn.micro caliburn

首先,我有这种情况:

        <components:ImageButton ImageSource="../Resources/Images/test.png" Height="32" Width="32" Style="{StaticResource MenuButtonWithContextMenuStyle}">
            <components:ImageButton.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Test" cal:Message.Attach="Test"/>
                </ContextMenu>
            </components:ImageButton.ContextMenu>
        </components:ImageButton>

这种风格:

        <Style TargetType="{x:Type components:ImageButton}" x:Key="MenuButtonWithContextMenuStyle">
            <Setter Property="ToolTipService.IsEnabled" Value="False"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type components:ImageButton}">
                        <Grid x:Name="ContentGrid" Background="{TemplateBinding Background}" ToolTip="{TemplateBinding ToolTip}" ContextMenu="{TemplateBinding ContextMenu}">
                            <Image Source="{TemplateBinding ImageSource}" Height="12" Width="12" HorizontalAlignment="Center" IsEnabled="{TemplateBinding IsEnabled}" ToolTip="{TemplateBinding ToolTip}" Stretch="Uniform" VerticalAlignment="Center" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True" />
                                    <Condition Property="IsEnabled" Value="True" />
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="#22000000" TargetName="ContentGrid"/>
                            </MultiTrigger>

                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsPressed" Value="True" />
                                    <Condition Property="IsEnabled" Value="True" />
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="#33000000" TargetName="ContentGrid"/>
                            </MultiTrigger>

                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding ElementName=ContentGrid, Path=ContextMenu.IsOpen}" Value="True"/>
                                    <Condition Binding="{Binding ElementName=ContentGrid, Path=IsEnabled}" Value="True"/>
                                </MultiDataTrigger.Conditions>
                                <Setter Property="Background" Value="#22000000" TargetName="ContentGrid"/>
                            </MultiDataTrigger>

                            <EventTrigger RoutedEvent="Click">                                    
                                <EventTrigger.Actions>
                                    <BeginStoryboard>                                            
                                        <Storyboard>
                                            <BooleanAnimationUsingKeyFrames Storyboard.Target="{Binding ElementName=ContentGrid}" Storyboard.TargetProperty="ContextMenu.IsOpen" >
                                                <DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
                                            </BooleanAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </EventTrigger.Actions>
                            </EventTrigger>

                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Opacity" Value="0.3"/>
                </Trigger>
            </Style.Triggers>
        </Style>

一切都非常基本,但我无法让Caliburn找到我的'Test'方法的目标。如果我将上下文菜单添加到style()而不是使用templatebinding,我可以让它工作,但这是我计划使用了很多次的按钮,如果我不得不复制它,我会讨厌它多次。

我已经在这里尝试了几个建议的修复程序和caliburn网站(例如TargetWithoutContext方法),但它永远不会有效。我不知道如何解决这个问题。

ImageButton是一个常规按钮,添加了ImageSource依赖属性。

2 个答案:

答案 0 :(得分:2)

好的,我要回答我自己的问题。

你能做的就是用这个: BindingProxy (thanks to Daniel)

然后,使用原样:

  

cal:Action.Target =&#34; {Binding Source = {StaticResource BindingProxy},Path = Data}&#34;

答案 1 :(得分:0)

Windows Phone 7中存在同样的问题,解决方案为here

基本上,您希望根据其他答案的建议使用cm:Action.TargetWithoutContext设置BindingProxy,或使用ElementName绑定。