WPF功能区绑定到ViewModel HierarchicalDataTemplate奇怪的行为

时间:2018-01-11 09:50:44

标签: c# wpf ribbon hierarchicaldatatemplate

我的计划是构建一个包含多个用户控件的大型WPF应用程序作为需要时加载的模块。这些模块提供了一个自己的菜单项列表,可以在主窗口中显示。

提供的菜单是MenuTab对象列表。

public class MenuTab
{
    private string _label;
    private List<MenuGroup> _menuGroups = new List<MenuGroup>();

    public string Label
    {
        get { return _label; }
        set { _label = value; }
    }

    public List<MenuGroup> MenuGroups
    {
        get { return _menuGroups; }
    }

    public MenuTab(string label)
    {
        Label = label;
    }
}

public class MenuGroup
{
    private string _label;
    private string _description;

    private List<MenuEntry> _menuEntries = new List<MenuEntry>();

    public string Label
    {
        get { return _label; }
        set { _label = value; }
    }

    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }

    public List<MenuEntry> MenuEntries
    {
        get { return _menuEntries; }
    }

    public MenuGroup(string label)
    {
        Label = label;
    }
}

public class MenuEntry
{
    private string _label;
    private BitmapSource _largeImage;
    private BitmapSource _smallImage;
    private ICommand _command;

    public string Label
    {
        get { return _label; }
        set { _label = value; }
    }

    public BitmapSource LargeImage
    {
        get { return _largeImage; }
        set { _largeImage = value; }
    }


    public BitmapSource SmallImage
    {
        get { return _smallImage; }
        set { _smallImage = value; }
    }

    public ICommand Command
    {
        get { return _command; }
        set { _command = value; }
    }

    public MenuEntry(string label)
    {
        Label = label;
    }
}

我在互联网上找到了多个提示,如何构建菜单以添加其他静态菜单等等。

这里我是如何构建菜单和HierarchicalDataTemplate的,它基本上产生了正确数量的选项卡,组和项目。 甚至命令绑定也能正常工作。

<RibbonWindow.Resources>
    <Style x:Key="ModuleGroup" TargetType="RibbonGroup">
        <Setter Property="Background" Value="AntiqueWhite" />
        <Setter Property="Foreground" Value="Green" />
    </Style>

    <HierarchicalDataTemplate DataType="{x:Type CommonModel:MenuTab}" ItemsSource="{Binding Path=MenuGroups}">
        <RibbonTab Header="{Binding Path=Label}" Background="Orange" />
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type CommonModel:MenuGroup}" ItemsSource="{Binding Path=MenuEntries}">
        <RibbonGroup Header="{Binding Path=Label}" Style="{StaticResource ModuleGroup}" />
    </HierarchicalDataTemplate>

    <DataTemplate DataType="{x:Type CommonModel:MenuEntry}">
        <RibbonButton Label="{Binding Path=Label}" LargeImageSource="{Binding Path=LargeImage}" SmallImageSource="{Binding Path=SmallImage}" Command="{Binding Path=Command}" />
    </DataTemplate>

    <CollectionViewSource x:Key="ModuleMenuTabs" Source="{Binding ModuleMenu}"/>
</RibbonWindow.Resources>

然后在我的静态标签之间:

<CollectionContainer Collection="{Binding Source={StaticResource ModuleMenuTabs}}"/>

但是标签标题和背景不会显示为例外。并且生成的组的背景也不是例外(例如我向其中一个静态组添加了一些背景,这些静态组工作正常。)

enter image description here

有人能给我一个如何正确生成色带的提示吗?

1 个答案:

答案 0 :(得分:0)

现在有了回复后,我自己做了进一步的研究和测试。 我发现解决方案对我有用 - 也许这有助于其他人。

XAML中的我的资源部分看起来像这样:

<RibbonWindow.Resources>
<DataTemplate x:Key="buttonTempl">
    <RibbonButton Label="{Binding Path=Label}" LargeImageSource="{Binding Path=LargeImage}" SmallImageSource="{Binding Path=SmallImage}" Command="{Binding Path=Command}" />
</DataTemplate>

<Style TargetType="RibbonGroup" x:Key="groupStyle">
    <Setter Property="Header" Value="{Binding Label}"/>
    <Setter Property="ItemsSource" Value="{Binding MenuEntries}"/>
    <Setter Property="ItemTemplate" Value="{StaticResource buttonTempl}"/>
</Style>
<Style TargetType="RibbonTab" x:Key="tabStyle">
    <Setter Property="Header" Value="{Binding Label}"/>
    <Setter Property="ItemsSource" Value="{Binding MenuGroups}"/>
    <Setter Property="ItemContainerStyle" Value="{StaticResource groupStyle}"/>
</Style>

<CollectionViewSource x:Key="StaticMenuSrc" Source="{Binding StaticMenu}"/>
<CollectionViewSource x:Key="ModuleMenuSrc" Source="{Binding ModuleMenu}"/>

功能区定义为:

<Ribbon DockPanel.Dock="Top" ItemContainerStyle="{StaticResource tabStyle}" >
        <Ribbon.ItemsSource>
            <CompositeCollection>
                <CollectionContainer Collection="{Binding Source={StaticResource StaticMenuSrc}}" />
                <CollectionContainer Collection="{Binding Source={StaticResource ModuleMenuSrc}}" />
            </CompositeCollection>
        </Ribbon.ItemsSource>
    </Ribbon>

这样我可以使用MVVM提供静态应用程序菜单和特定于模块的菜单。