菜单作为棱镜区域 +主题问题

发布于 2024-10-06 10:02:06 字数 137 浏览 2 评论 0原文

我使用菜单控件作为区域。它工作正常,但是当我向应用程序添加主题(向我的 App.xaml 添加资源字典)时,正常菜单具有该主题,但我区域内的视图(菜单项)没有该主题。

其背后的原因可能是什么?有人有解决方法的想法吗?

亚历克斯.

I am using a Menu control as a region. It works fine, however when I add a theme to my application (Added a resource-dictionary to my App.xaml) normal menus have the theme, but the views inside my region (which are menu-items) don't have this theme.

What might be the reason behind it? Anybody has an idea for a work-around?

Alex.

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

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

发布评论

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

评论(3

她如夕阳 2024-10-13 10:02:06

问题在于,当在 MenuItem 内使用该区域时,该区域的 ItemsControl 会设置样式,但其子项不会设置样式。我通过使用来自各种来源的动态菜单找到了解决方法:

WPF 示例系列 – 数据绑定 HierarchicalDataTemplate 菜单示例

使用 HierarchicalDataTemplate 构建数据绑定 WPF 菜单

我在基础结构项目中创建了 IMenuService 和 MenuItemViewModel,在 Shell 项目中实现 IMenuService,然后使用IMenuService 在我的模块中添加菜单。

public interface IMenuService
{
    List<MenuItemViewModel> Menu { get; set; }
    MenuItemViewModel FileMenu { get; set; }
}

public class MenuItemViewModel : NotificationObject
{
    private string _header;
    private bool _isEnabled = true;
    private ICommand _command;
    private Image _icon;
    private string _inputGestureText;
    private ObservableCollection<MenuItemViewModel> _children;

    public MenuItemViewModel()
    {
        Children = new ObservableCollection<MenuItemViewModel>();
    }

    public MenuItemViewModel(bool isSeparator) : this()
    {
        _isSeparator = isSeparator;
    }

    public string Header
    {
        get { return _header; }
        set
        {
            _header = value;
            RaisePropertyChanged("Header");
        }
    }

    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            _isEnabled = value;
            RaisePropertyChanged("IsEnabled");
        }
    }

    public ICommand Command
    {
        get { return _command; }
        set
        {
            _command = value;
            RaisePropertyChanged("Command");
        }
    }

    public Image Icon
    {
        get { return _icon; }
        set
        {
            _icon = value;
            RaisePropertyChanged("Icon");
        }
    }

    public string InputGestureText
    {
        get { return _inputGestureText; }
        set
        {
            _inputGestureText = value;
            RaisePropertyChanged("InputGestureText");
        }
    }

    public ObservableCollection<MenuItemViewModel> Children
    {
        get { return _children; }
        set { _children = value; }
    }

    private bool _isSeparator;
    public bool IsSeparator
    {
        get { return _isSeparator; }
        set { _isSeparator = value; }
    }
}

Shell 项目中的 MenuService:

[Export(typeof(IMenuService))]
public class MenuService : IMenuService
{
    [ImportingConstructor]
    public MenuService()
    {
        _menu = new List<MenuItemViewModel>();
        _fileMenu = new MenuItemViewModel {Header = "_File"};
        _fileMenu.Children.Add(new MenuItemViewModel { Header = "_New" });
        _fileMenu.Children.Add(new MenuItemViewModel(true)); // Separator

        _menu.Add(_fileMenu);
    }

    private static Image CreateImage(string url)
    {
        var image = new Image
                        {
                            Source = new BitmapImage(new Uri("Resources/" + url, UriKind.Relative)),
                            Height = 16,
                            Width = 16
                        };
        RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor);
        return image;
    }

    private List<MenuItemViewModel> _menu;
    public List<MenuItemViewModel> Menu
    {
        get { return _menu; }
        set { _menu = value; }
    }

    private MenuItemViewModel _fileMenu;
    public MenuItemViewModel FileMenu
    {
        get { return _fileMenu; }
        set { _fileMenu = value; }
    }
}

在 Shell.xaml 中

<Window.Resources>
    <Style TargetType="{x:Type MenuItem}" x:Key="separatorStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MenuItem}">
                    <Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style BasedOn="{StaticResource {x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
        <Setter Property="Command" Value="{Binding Command}"/>
        <Setter Property="Icon" Value="{Binding Icon}"/>
        <Setter Property="InputGestureText" Value="{Binding InputGestureText}"/>
        <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
    </Style>

    <HierarchicalDataTemplate DataType="{x:Type infrastructure:MenuItemViewModel}" ItemsSource="{Binding Path=Children, UpdateSourceTrigger=PropertyChanged}">
        <ContentPresenter Content="{Binding Header}" RecognizesAccessKey="True" />
    </HierarchicalDataTemplate>

    <local:SeparatorStyleSelector x:Key="separatorStyleSelector"/>

在 ShellViewModel 中:

[ImportingConstructor]
    public ShellViewModel(IMenuService menuService)
    {
        Menu = menuService.Menu.ToObservableCollection();
    }

    private ObservableCollection<MenuItemViewModel> _menu = new ObservableCollection<MenuItemViewModel>();
    public ObservableCollection<MenuItemViewModel> Menu
    {
        get { return _menu; }
        set
        {
            _menu = value;
            RaisePropertyChanged("MenuItems");
        }
    }

从模块添加菜单:

[ModuleExport(typeof(OptionModule))]
public class OptionModule : IModule
{
    [ImportingConstructor]
    public OptionModule(IMenuService menuService)
    {
        menuService.ToolsMenu.Children.Add(new MenuItemViewModel {Header = "Options", Command = OptionCommand});
    }
}

这是上面 Karl 站点中的 SeparatorStyleSelector:

public override Style SelectStyle(object item, DependencyObject container)
    {
        if (((MenuItemViewModel)item).IsSeparator)
        {
            return (Style) ((FrameworkElement) container).FindResource("separatorStyle");
        }
        return base.SelectStyle(item, container);
    }
enter code here

The problem is that the ItemsControl of the region gets styled but its children do not when the region is used inside a MenuItem. I found a workaround by using dynamic menus from various sources:

WPF Sample Series – Databound HierarchicalDataTemplate Menu Sample

Building a Databound WPF Menu Using a HierarchicalDataTemplate

I created an IMenuService and MenuItemViewModel in my Infrastructure project, implement the IMenuService in my Shell project, and use IMenuService in my module to add a menu.

public interface IMenuService
{
    List<MenuItemViewModel> Menu { get; set; }
    MenuItemViewModel FileMenu { get; set; }
}

public class MenuItemViewModel : NotificationObject
{
    private string _header;
    private bool _isEnabled = true;
    private ICommand _command;
    private Image _icon;
    private string _inputGestureText;
    private ObservableCollection<MenuItemViewModel> _children;

    public MenuItemViewModel()
    {
        Children = new ObservableCollection<MenuItemViewModel>();
    }

    public MenuItemViewModel(bool isSeparator) : this()
    {
        _isSeparator = isSeparator;
    }

    public string Header
    {
        get { return _header; }
        set
        {
            _header = value;
            RaisePropertyChanged("Header");
        }
    }

    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            _isEnabled = value;
            RaisePropertyChanged("IsEnabled");
        }
    }

    public ICommand Command
    {
        get { return _command; }
        set
        {
            _command = value;
            RaisePropertyChanged("Command");
        }
    }

    public Image Icon
    {
        get { return _icon; }
        set
        {
            _icon = value;
            RaisePropertyChanged("Icon");
        }
    }

    public string InputGestureText
    {
        get { return _inputGestureText; }
        set
        {
            _inputGestureText = value;
            RaisePropertyChanged("InputGestureText");
        }
    }

    public ObservableCollection<MenuItemViewModel> Children
    {
        get { return _children; }
        set { _children = value; }
    }

    private bool _isSeparator;
    public bool IsSeparator
    {
        get { return _isSeparator; }
        set { _isSeparator = value; }
    }
}

MenuService in Shell project:

[Export(typeof(IMenuService))]
public class MenuService : IMenuService
{
    [ImportingConstructor]
    public MenuService()
    {
        _menu = new List<MenuItemViewModel>();
        _fileMenu = new MenuItemViewModel {Header = "_File"};
        _fileMenu.Children.Add(new MenuItemViewModel { Header = "_New" });
        _fileMenu.Children.Add(new MenuItemViewModel(true)); // Separator

        _menu.Add(_fileMenu);
    }

    private static Image CreateImage(string url)
    {
        var image = new Image
                        {
                            Source = new BitmapImage(new Uri("Resources/" + url, UriKind.Relative)),
                            Height = 16,
                            Width = 16
                        };
        RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor);
        return image;
    }

    private List<MenuItemViewModel> _menu;
    public List<MenuItemViewModel> Menu
    {
        get { return _menu; }
        set { _menu = value; }
    }

    private MenuItemViewModel _fileMenu;
    public MenuItemViewModel FileMenu
    {
        get { return _fileMenu; }
        set { _fileMenu = value; }
    }
}

In Shell.xaml

<Window.Resources>
    <Style TargetType="{x:Type MenuItem}" x:Key="separatorStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MenuItem}">
                    <Separator HorizontalAlignment="Stretch" IsEnabled="False"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <Style BasedOn="{StaticResource {x:Type MenuItem}}" TargetType="{x:Type MenuItem}">
        <Setter Property="Command" Value="{Binding Command}"/>
        <Setter Property="Icon" Value="{Binding Icon}"/>
        <Setter Property="InputGestureText" Value="{Binding InputGestureText}"/>
        <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
    </Style>

    <HierarchicalDataTemplate DataType="{x:Type infrastructure:MenuItemViewModel}" ItemsSource="{Binding Path=Children, UpdateSourceTrigger=PropertyChanged}">
        <ContentPresenter Content="{Binding Header}" RecognizesAccessKey="True" />
    </HierarchicalDataTemplate>

    <local:SeparatorStyleSelector x:Key="separatorStyleSelector"/>

In the ShellViewModel:

[ImportingConstructor]
    public ShellViewModel(IMenuService menuService)
    {
        Menu = menuService.Menu.ToObservableCollection();
    }

    private ObservableCollection<MenuItemViewModel> _menu = new ObservableCollection<MenuItemViewModel>();
    public ObservableCollection<MenuItemViewModel> Menu
    {
        get { return _menu; }
        set
        {
            _menu = value;
            RaisePropertyChanged("MenuItems");
        }
    }

Adding a menu from the module:

[ModuleExport(typeof(OptionModule))]
public class OptionModule : IModule
{
    [ImportingConstructor]
    public OptionModule(IMenuService menuService)
    {
        menuService.ToolsMenu.Children.Add(new MenuItemViewModel {Header = "Options", Command = OptionCommand});
    }
}

Here is the SeparatorStyleSelector from Karl's site above:

public override Style SelectStyle(object item, DependencyObject container)
    {
        if (((MenuItemViewModel)item).IsSeparator)
        {
            return (Style) ((FrameworkElement) container).FindResource("separatorStyle");
        }
        return base.SelectStyle(item, container);
    }
enter code here
筱武穆 2024-10-13 10:02:06

这可能是 Prism 菜单区域适配器中的错误。我建议在 CodePlex 上报告该问题。

It could be a bug in the Prism Menu region adapter. I'd suggest reporting the issue on CodePlex.

梦里梦着梦中梦 2024-10-13 10:02:06

我只会迭代子控件并明确地将主题分配给每个子控件。

I would just iterate the children controls and assign the theme to each of them explicitly.

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