Search code examples
c#wpfcontextmenucaliburn.microcaliburn

Caliburn Micro and ContextMenu results in target not found


To start with, I have this situation:

        <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>

Along with this style:

        <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>

All pretty basic, but I cannot get Caliburn to find the target of my 'Test' method. I can get it to work if I add the context menu to the style ( ) instead of using the templatebinding, but this is a button I'm planning on using quite a few times and I would hate it if I had to copy this over multiple times.

I already tried several suggested fixes on here and caliburn site (such as the TargetWithoutContext method), but it never works. I have no idea anymore on how I would be able to fix this.

ImageButton is a regular button with an ImageSource dependency property added.


Solution

  • Alright, I'm going to post the answer to my own question.

    What you can do is use this: BindingProxy (thanks to Daniel)

    Then, use as such:

    cal:Action.Target="{Binding Source={StaticResource BindingProxy}, Path=Data}"