MenuItem lost DataContext

Topics: Actions & Coroutines
Oct 17, 2011 at 8:08 AM

Hi,

I have this style into ResourceDictionary. This is a ToggleButton´s list. I add a contextMenu to each button and I want to hand into the viewmodel the click event. I have the method Editindicator into viewmodel. When I run the project and i click over contextmenu item it broke and show this error "{"No target found for method Click."}". I think that this error is owing to menuitem has lost viewmodel´s datacontext. Can anyone help here? Thanks a lot in advice.

 

<Style x:Key="ListBoxStyleIndicador" TargetType="{x:Type ListBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Padding" Value="2,0,2,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate x:Name="ListBoxStyleIndicadorTemplate" TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            Padding="{TemplateBinding Padding}"
                            SnapsToDevicePixels="true">
                        <Controles:ToggleButtonIndicador
                            Content="{Binding NombreIndicador}"
                            IdBIIndicadores="{Binding IdBiIndicadores}"
                            IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                            Style="{DynamicResource BotonNegro}"
                            Padding="6,2"                            
                            ToolTip="{Binding Descripcion}">
                            <Controles:ToggleButtonIndicador.ContextMenu >
                                <ContextMenu>
                                    <MenuItem Header="Editar">
                                        <MenuItem.Icon>
                                            <Image Source="{DynamicResource ImagenBotonEditar}" />                                           
                                        </MenuItem.Icon>
                                        <Interactivity:Interaction.Triggers>
                                            <Interactivity:EventTrigger EventName="Click">
                                                <cal:ActionMessage MethodName="EditIndicator" />
                                            </Interactivity:EventTrigger>
                                        </Interactivity:Interaction.Triggers>
                                    </MenuItem>                                 
                                </ContextMenu>
                            </Controles:ToggleButtonIndicador.ContextMenu>
                        </Controles:ToggleButtonIndicador>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="IsSelected" Value="{Binding Seleccionado, Mode=TwoWay}"/>       
    </Style>

Oct 17, 2011 at 11:51 AM

Solution:
[WPF] How to bind to data when the DataContext is not inherited
http://tomlev2.wordpress.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

 

 <Style x:Key="ListBoxStyleIndicador" TargetType="{x:Type ListBoxItem}">
        <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
        <Setter Property="Padding" Value="2,0,2,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate x:Name="ListBoxStyleIndicadorTemplate" TargetType="{x:Type ListBoxItem}">
                    <Border x:Name="Bd"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            Background="{TemplateBinding Background}"
                            Padding="{TemplateBinding Padding}"
                            SnapsToDevicePixels="true">
                        <Controles:ToggleButtonIndicador
                            Content="{Binding NombreIndicador}"
                            IdBIIndicadores="{Binding IdBiIndicadores}"
                            IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}"
                            Style="{DynamicResource BotonNegro}"
                            Padding="6,2"                            
                            ToolTip="{Binding Descripcion}">

                            <Controles:ToggleButtonIndicador.Resources>
                                <Controles:BindingProxy x:Key="proxy" Data="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
                            </Controles:ToggleButtonIndicador.Resources>

                            <Controles:ToggleButtonIndicador.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="Editar" DataContext="{Binding Data,Source={StaticResource proxy}}">
                                        <MenuItem.Icon>
                                            <Image Source="{DynamicResource ImagenBotonEditar}" />
                                        </MenuItem.Icon>
                                        <Interactivity:Interaction.Triggers>
                                            <Interactivity:EventTrigger EventName="Click" >
                                                <cal:ActionMessage MethodName="EditarIndicador" >
                                                </cal:ActionMessage >
                                            </Interactivity:EventTrigger>
                                        </Interactivity:Interaction.Triggers>
                                    </MenuItem>                                   
                                </ContextMenu>
                            </Controles:ToggleButtonIndicador.ContextMenu>

                        </Controles:ToggleButtonIndicador>
                    </Border>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="IsSelected" Value="{Binding Seleccionado, Mode=TwoWay}"/>
    </Style>