我怎样才能prevent从完全崩溃,而无需指定的大小定义的所有组合的WPF色带组?组合、色带、定义、大小

2023-09-06 17:08:38 作者:等一颗真心

我怎样才能prevent从完全倒塌WPF的丝带组?

How can I prevent a wpf ribbon group from fully collapsing?

我知道有一个RibbonGroup.GroupSizeDefinitions属性(它在这里讨论: HTTP: //msdn.microsoft.com/en-us/library/ff701790.aspx ),如果我定义了一堆RibbonGroupSizeDefinition并没有与isCollapsed = true,则接近我想要的。

I realise there is a RibbonGroup.GroupSizeDefinitions property (it's discussed here: http://msdn.microsoft.com/en-us/library/ff701790.aspx) and if I define a bunch of RibbonGroupSizeDefinition and none with isCollapsed=true it does close to what I want.

不幸的是,我不希望有限定的尺寸的定义,因为示出将根据用户的许可证而变化的按钮。例如。一个客户端可以得到六个按钮在一组,一个可能有四种。

Unfortunately I don't want to have to define the size definitions because the buttons shown will vary depending on the users license. eg. one client may get six buttons in a group and one might have four.

所以,我希望它自动设置组,但从来没有进入完全崩溃了 - 这是可能的。

So I want it to auto setup the groups but never go into fully collapsed - is this possible?

推荐答案

我已经得出丝带,RibbonTab和RibbonGroup。

I have derived Ribbon, RibbonTab and RibbonGroup.

相反,我使用反射来访问GroupSizeDefinitions属性。

Instead of rewriting the code of GroupSizeDefinitions, I use reflection to access the GroupSizeDefinitions property.

public class xxxRibbon : Ribbon
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new xxxRibbonTab();
    }
}

public class xxxRibbonTab : RibbonTab
{
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new xxxRibbonGroup();
    }
}

public class xxxRibbonGroup : RibbonGroup
{
    public const string PropertyName_IsCollapsable = "IsCollapsable";
    public const string PropertyName_GroupSizeDefinitionsResourceName = "GroupSizeDefinitionsResourceName";

    public static readonly DependencyProperty IsCollapsableProperty =
        DependencyProperty.Register(xxxRibbonGroup.PropertyName_IsCollapsable, typeof(bool), typeof(xxxRibbonGroup), new FrameworkPropertyMetadata((bool)true, xxxRibbonGroup.IsCollapsablePropertyChangedCallback));

    public static readonly DependencyProperty GroupSizeDefinitionsResourceNameProperty =
        DependencyProperty.Register(xxxRibbonGroup.PropertyName_GroupSizeDefinitionsResourceName, typeof(string), typeof(xxxRibbonGroup), new FrameworkPropertyMetadata((string)null, xxxRibbonGroup.GroupSizeDefinitionsResourceNamePropertyChangedCallback));

    public bool IsCollapsable
    {
        get { return (bool)this.GetValue(xxxRibbonGroup.IsCollapsableProperty); }
        set { this.SetValue(xxxRibbonGroup.IsCollapsableProperty, value); }
    }

    public string GroupSizeDefinitionsResourceName
    {
        get { return (string)this.GetValue(xxxRibbonGroup.GroupSizeDefinitionsResourceNameProperty); }
        set { this.SetValue(xxxRibbonGroup.GroupSizeDefinitionsResourceNameProperty, value); }
    }

    private static void IsCollapsablePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        xxxRibbonGroup ribbonGroup = dependencyObject as xxxRibbonGroup;
        if (ribbonGroup == null)
        {
            return;
        }

        ribbonGroup.rebuildGroupSizeDefinitions();
    }

    private static void GroupSizeDefinitionsResourceNamePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        xxxRibbonGroup ribbonGroup = dependencyObject as xxxRibbonGroup;
        if (ribbonGroup == null)
        {
            return;
        }

        ribbonGroup.rebuildGroupSizeDefinitions();
    }

    protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
    {
        base.OnItemsChanged(e);

        this.GroupSizeDefinitions = null;
        this.rebuildGroupSizeDefinitions();
    }

    protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
    {
        base.OnItemsSourceChanged(oldValue, newValue);

        this.GroupSizeDefinitions = null;
        this.rebuildGroupSizeDefinitions();
    }

    private void rebuildGroupSizeDefinitions()
    {
        string resourceName = this.GroupSizeDefinitionsResourceName;
        if (string.IsNullOrEmpty(resourceName) == false)
        {
            object resource = Application.Current.TryFindResource(resourceName);
            if (resource == null)
            {
                this.GroupSizeDefinitions = null;
                return;
            }

            RibbonGroupSizeDefinitionBaseCollection grsidef = resource as RibbonGroupSizeDefinitionBaseCollection;
            if (grsidef == null)
            {
                this.GroupSizeDefinitions = null;
                return;
            }

            this.GroupSizeDefinitions = grsidef;
            return;
        }

        PropertyInfo[] props = this.GetType().BaseType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

        foreach (PropertyInfo propertyInfo in props)
        {
            if (propertyInfo.Name == "GroupSizeDefinitionsInternal")
            {
                RibbonGroupSizeDefinitionBaseCollection value = propertyInfo.GetValue(this, BindingFlags.GetProperty | BindingFlags.NonPublic, null, null, null) as RibbonGroupSizeDefinitionBaseCollection;
                if ((value != null) && (value.Count > 0))
                {
                    RibbonGroupSizeDefinitionBaseCollection result = new RibbonGroupSizeDefinitionBaseCollection();
                    foreach (RibbonGroupSizeDefinitionBase it in value)
                    {
                        if (this.IsCollapsable == false)
                        {
                            if (it.IsCollapsed == false)
                            {
                                result.Add(it);
                            }
                        }
                        else
                        {
                            result.Add(it);
                        }
                    }

                    result.Freeze();

                    this.GroupSizeDefinitions = result;
                    return;
                }

                return;
            }
        }

        Traceur.Assert(false);
    }
}