不手动 MenuItem Click 进入视图模型

发布于 2024-12-10 07:11:51 字数 3192 浏览 1 评论 0原文

我把这种风格放入ResourceDictionary中。这是一个 ToggleButton 的列表。我向每个按钮添加一个 contextMenu,并且我想将单击事件传递到视图模型中。我有方法 Editindicator 进入视图模型。当我运行该项目并单击上下文菜单项时,它损坏并显示此错误“{“找不到方法 Click 的目标。”}”。我认为这个错误是由于 menuitem 丢失了 viewmodel 的数据上下文。 有人可以帮忙吗?非常感谢您的建议。

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

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>

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

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

发布评论

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

评论(2

╰沐子 2024-12-17 07:11:51

由于 ContextMenu 不是 Visual 树的一部分,因此您不会获得对为 UserControl 设置的 ViewModel 的 DataContext 的引用。有两种方法可以获取控件的数据上下文 -

编辑:对于 DataContext 你可以这样做 -

<Controles:ToggleButtonIndicador Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}>
                        <Controles:ToggleButtonIndicador.ContextMenu >
                            <ContextMenu DataContext={Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}>
                                <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>

我假设你的 viemodel 是绑定到 UserControl 的 DataContext 属性。

Since ContextMenu is not a part of your Visual tree, you won't get reference to the DataContext of your ViewModel which is set for your UserControl. There are two ways to get the datacontext for your control -

Edit : For DataContext you can do like this -

<Controles:ToggleButtonIndicador Tag="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}>
                        <Controles:ToggleButtonIndicador.ContextMenu >
                            <ContextMenu DataContext={Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}>
                                <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>

I am assumsing that your viemodel is binded to DataContext property of your UserControl.

骄兵必败 2024-12-17 07:11:51

万一它可能对任何人有帮助。在使用以下示例代码之前,强烈建议阅读 将虚拟分支附加到 CodeProject 上的 WPF 中的逻辑树。你就会知道为什么会出现问题以及如何优雅地解决它。

这是一个简单的例子。

// Add a DataContextBridge.
<UserControl.Resources>
    <FrameworkElement x:Key="DataContextBridge" />
</UserControl.Resources>

// Bind.
<UserControl.DataContext>
    <Binding
        Mode="OneWayToSource"
        Path="DataContext"
        Source="{StaticResource DataContextBridge}" />
</UserControl.DataContext>

// Trigger a click event.
<ContextMenu>
    <MenuItem>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <ei:CallMethodAction
                    TargetObject="{Binding Source={StaticResource DataContextBridge}, Path=DataContext}"
                    MethodName="OnClick" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </MenuItem>
</ContextMenu>

谢谢。

In case it might help anyone. Before using the following example code, it is highly recommended to read Attaching a Virtual Branch to the Logical Tree in WPF on CodeProject. You would know why the problem occurs and how to solve it elegantly.

Here is the quick example.

// Add a DataContextBridge.
<UserControl.Resources>
    <FrameworkElement x:Key="DataContextBridge" />
</UserControl.Resources>

// Bind.
<UserControl.DataContext>
    <Binding
        Mode="OneWayToSource"
        Path="DataContext"
        Source="{StaticResource DataContextBridge}" />
</UserControl.DataContext>

// Trigger a click event.
<ContextMenu>
    <MenuItem>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <ei:CallMethodAction
                    TargetObject="{Binding Source={StaticResource DataContextBridge}, Path=DataContext}"
                    MethodName="OnClick" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </MenuItem>
</ContextMenu>

Thanks.

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