1

如何防止 wpf 功能区组完全折叠?

我意识到有一个 RibbonGroup.GroupSizeDefinitions 属性(在这里讨论:http: //msdn.microsoft.com/en-us/library/ff701790.aspx),如果我定义了一堆 RibbonGroupSizeDefinition 而没有使用 isCollapsed=true 它确实接近我想要的。

不幸的是,我不想定义尺寸定义,因为显示的按钮会因用户许可证而异。例如。一个客户可能会在一组中获得六个按钮,而一个客户可能有四个。

所以我希望它自动设置组但永远不会完全折叠 - 这可能吗?

4

3 回答 3

2

试试这个 :

<RibbonGroup Name="Clipboard">
   <RibbonGroup.GroupSizeDefinitions>
       <RibbonGroupSizeDefinition>
            <RibbonControlSizeDefinition ImageSize="Large" IsLabelVisible="True" />
            <RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="True" />
            <RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="True" />
        </RibbonGroupSizeDefinition>           
           <RibbonGroupSizeDefinition>
             <RibbonControlSizeDefinition ImageSize="Large" IsLabelVisible="True" />
             <RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="False" />
             <RibbonControlSizeDefinition ImageSize="Small" IsLabelVisible="False" />
           </RibbonGroupSizeDefinition>
          <RibbonGroupSizeDefinition IsCollapsed="False" />
    </RibbonGroup.GroupSizeDefinitions>
 <RibbonGroup>

再也不会崩溃了。

于 2018-06-22T08:39:33.133 回答
0

我看到功能区源代码可供下载http://www.microsoft.com/en-au/download/details.aspx?id=11877

从这里我可以查看 RibbonGroup 内部,并看到它有一个私有属性 GroupSizeDefinitionsInternal 可以自动调整大小。它总是添加一个定义,IsCollapsed = true所以没有办法按照我的要求去做。

一个解决方案是在您的代码中编写代码以实现您的所有大小定义,但没有IsCollapsed = true- 请注意,许可证声明您不能使用或修改现有代码,因此需要从头开始编写,而不是从属性中复制。

于 2012-09-18T00:14:22.970 回答
0

我已经派生了 Ribbon、RibbonTab 和 RibbonGroup。

我没有重写 GroupSizeDefinitions 的代码,而是使用反射来访问 GroupSizeDefinitions 属性。

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);
    }
}
于 2013-03-06T11:43:02.087 回答