wpf - 我可以使用 DataTemplate 将 ContextMenu 应用于 ContextMenuViewModel 吗?

标签 wpf mvvm datatemplate contextmenu

我有一个 ViewModel (AbstractContextMenu) 代表我的上下文菜单 (IContextMenu),我使用 DataTemplate 将一个真正的 ContextMenu 绑定(bind)到它:

<DataTemplate DataType="{x:Type local:AbstractContextMenu}">
    <ContextMenu x:Name="contextMenu" 
          ItemsSource="{Binding Path=(local:IContextMenu.Items)}"
          IsEnabled="{Binding Path=(local:IContextMenu.IsEnabled)}"/>
</DataTemplate>

然后我有一个虚拟的 ConcreteContextMenu 用于测试,它只是继承自 AbstractContextMenu。 AbstractContextMenu 只是实现了这个接口(interface):
public interface IContextMenu : IExtension
{
    IEnumerable<IMenuItem> Items { get; set; }
    bool IsEnabled { get; set; }
}

我将它用作另一个 ViewModel 对象的属性:
    public IContextMenu ContextMenu
    {
        get
        {
            return m_ContextMenu;
        }
        protected set
        {
            if (m_ContextMenu != value)
            {
                m_ContextMenu = value;
                NotifyPropertyChanged(m_ContextMenuArgs);
            }
        }
    }
    private IContextMenu m_ContextMenu = new ConcreteContextMenu();
    static readonly PropertyChangedEventArgs m_ContextMenuArgs =
        NotifyPropertyChangedHelper.CreateArgs<AbstractSolutionItem>(o => o.ContextMenu);

然后我将 StackPanel 绑定(bind)到该 ViewModel 并将 StackPanel 上的 ContextMenu 属性绑定(bind)到 ViewModel 的 ContextMenu 属性:
    <StackPanel Orientation="Horizontal" 
                ContextMenu="{Binding Path=(local:AbstractSolutionItem.ContextMenu)}"
                ContextMenuOpening="stackPanel_ContextMenuOpening">
    <!-- stuff goes in here -->
    </StackPanel>

当我运行它时,StackPanel 上的 ContextMenuOpening 事件被触发,但 ContextMenu 永远不会显示。我不确定我是否能做到这一点(使用 DataTemplate 将 ContextMenu 应用于 ContextMenu ViewModel)。有人知道吗?

最佳答案

AbstractSolutionItem.ContextMenu的类型是什么?如果它对应于 ContextMenu您的问题中的属性,那么问题可能是类型错误。 ContextMenu FrameworkElement 的属性(property)期待一个实际的 ContextMenu ,而不是 IContextMenu .在调试您的应用程序时尝试检查输出窗口 - 您可能会收到一条错误消息,指出这是问题所在。

而不是使用 DataTemplate定义您的ContextMenu ,只要把模板的内容StackPanel.ContextMenu :

<StackPanel Orientation="Horizontal" 
    ContextMenu="{Binding Path=(local:AbstractSolutionItem.ContextMenu)}"
    ContextMenuOpening="stackPanel_ContextMenuOpening">
    <StackPanel.ContextMenu DataContext="{Binding Path=(local:AbstractSolutionItem.ContextMenu)}">
        <ContextMenu x:Name="contextMenu" 
            ItemsSource="{Binding Path=Items}"
            IsEnabled="{Binding Path=IsEnabled}"/>
    </StackPanel.ContextMenu>
    <!-- stuff goes in here -->
</StackPanel>

这应该让你大部分时间都在那里。但是,自从 ContextMenu 之后仍然存在问题。不知道如何创建 MenuItem来自 IMenuItem .为了解决这个问题,创建一个 ItemTemplate对于ContextMenu ,它绑定(bind) IMenuItem 的成员到`菜单项。

关于wpf - 我可以使用 DataTemplate 将 ContextMenu 应用于 ContextMenuViewModel 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2307650/

相关文章:

wpf - 从固定文档中删除页面?

wpf - 如何解决 AllowsTransparency=true 的 WPF 窗口中的 ActiveX WebBrowser 缺陷

c# - 如何使用 ReactiveUI 在 ViewModel 停用时正确取消任务?

c# - 如何在 WPF 项目中将 rdlc 文件添加到 ReportViewer

.net - 我需要 WPF 4 的 Accordion 控件

c# - 为什么适配器不工作?

wpf - MVVM 绑定(bind)到 InkCanvas

c# - 如何根据绑定(bind)对象的类型动态更改DataTemplate?

WPF 数据模板和按钮

c# - 从一个 DataTemplate 切换到另一个