禁用在点击时显示最小化的功能区选项卡

问题描述 投票:0回答:1

我使用Ribbon for WPF(2010 - Microsoft.Windows.Controls.Ribbon)。

Ribbon.IsMinimized设置为true时,色带最小化。

正常的行为是,当我单击最小化选项卡时,它将暂时打开。但有没有办法禁用它,以防止它扩大?

wpf popup ribbon minimize
1个回答
2
投票

请参阅下面的代码。我不清楚你想要什么,但我已经涵盖了2个可能满足你需求的行为案例。

  • 情况1: 如果功能区设置为最小化并单击功能区选项卡,它将关闭最小化标记,并从该点开始将功能区转换为非最小化标记。您可以双击选项卡,或从上下文菜单中选择最小化功能区以再次最小化。
  • 案例2: 如果在功能区最小化时单击功能区选项卡,它将使功能区保持最小化状态。

我提供了2个代码实现,一个只挂钩常规功能区,另一个创建派生功能区。

<Window x:Class="DemoOfRibbonWithDifferentMinimizeBehaviour.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Name="Main"
        Title="MainWindow" Height="350" Width="525" xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon">
    <Grid>
        <my:Ribbon Height="254" HorizontalAlignment="Left" Name="ribbon1" VerticalAlignment="Top" Width="503" ItemsSource="{Binding}" IsMinimized="True" IsCollapsed="False" IsDropDownOpen="False" Loaded="ribbon1_Loaded">
            <my:RibbonTab Header="Fruits" KeyTip="F">
                <my:RibbonGroup Header="Berries">
                    <my:RibbonButton Label="Grapes"/>
                    <my:RibbonButton Label="Bananas"/>
                </my:RibbonGroup>
                <my:RibbonGroup Header="Pome">
                    <my:RibbonButton Label="Apple"/>
                    <my:RibbonButton Label="Pears"/>
                </my:RibbonGroup>
            </my:RibbonTab>
            <my:RibbonTab Header="Vegetables" KeyTip="V">
                <my:RibbonGroup Header="Root">
                    <my:RibbonButton Label="Carrot"/>
                    <my:RibbonButton Label="Turnips"/>
                </my:RibbonGroup>
                <my:RibbonGroup Header="Stalk">
                    <my:RibbonButton Label="Bamboo"/>
                    <my:RibbonButton Label="Celery"/>
                </my:RibbonGroup>
            </my:RibbonTab>
            <my:RibbonCheckBox Label="Prevent Maximize" IsChecked="{Binding ElementName=Main, Path=PreventMaximize}">Prevent Maximize</my:RibbonCheckBox>
        </my:Ribbon>
    </Grid>
</Window>

窗口代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows.Controls.Primitives;
using System.ComponentModel;

namespace DemoOfRibbonWithDifferentMinimizeBehaviour
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Popup template_PART_ITEMSPRESENTERPOPUP;

        public bool PreventMaximize { get; set; }

        public MainWindow()
        {
            PreventMaximize = true;

            InitializeComponent();
        }

        private void ribbon1_Loaded(object sender, RoutedEventArgs e)
        {
            template_PART_ITEMSPRESENTERPOPUP = VisualHelper.FindChild<Popup>(ribbon1, "PART_ITEMSPRESENTERPOPUP");

            if (template_PART_ITEMSPRESENTERPOPUP != null)
            {
                template_PART_ITEMSPRESENTERPOPUP.Height = 0;
                template_PART_ITEMSPRESENTERPOPUP.StaysOpen = false;
                template_PART_ITEMSPRESENTERPOPUP.Opened += new EventHandler(template_PART_ITEMSPRESENTERPOPUP_Opened);
            }
        }

        void template_PART_ITEMSPRESENTERPOPUP_Opened(object sender, EventArgs e)
        {
            if (ribbon1.IsMinimized)
            {
                ribbon1.IsMinimized = false;
                ribbon1.IsDropDownOpen = false;

                template_PART_ITEMSPRESENTERPOPUP.Visibility = System.Windows.Visibility.Collapsed;
            }

            // Uncomment this if your intention is to prevent the Ribbon being maximized
            // i.e. keep it fixed in minimized mode.
            if (PreventMaximize)
            {
                ribbon1.IsMinimized = true;
            }
        }
    }

    //--------------------------------------------------------------------------

    // If you want to encapsulate the new behaviour in a new Ribbon class, then you
    // could use this instead of the above and modify your XAML appropriately.

    public class RibbonWhichPreventsMaximize : Ribbon
    {
        Popup template_PART_ITEMSPRESENTERPOPUP;
        EventHandler popupeventhandler;

        public RibbonWhichPreventsMaximize()
        {
            popupeventhandler = new EventHandler(template_PART_ITEMSPRESENTERPOPUP_Opened);
        }

        public override void OnApplyTemplate()
        {
            // Unwire the handler if this Ribbon control is getting a new Template applied
            // (e.g. for case when Template is dynamically changed at runtime).

            if (template_PART_ITEMSPRESENTERPOPUP != null)
            {
                template_PART_ITEMSPRESENTERPOPUP.Opened -= popupeventhandler;
            }

            base.OnApplyTemplate();

            template_PART_ITEMSPRESENTERPOPUP = VisualHelper.FindChild<Popup>(this, "PART_ITEMSPRESENTERPOPUP");

            if (template_PART_ITEMSPRESENTERPOPUP != null)
            {
                template_PART_ITEMSPRESENTERPOPUP.Height = 0;
                template_PART_ITEMSPRESENTERPOPUP.StaysOpen = false;
                template_PART_ITEMSPRESENTERPOPUP.Opened += popupeventhandler;
            }
        }

        void template_PART_ITEMSPRESENTERPOPUP_Opened(object sender, EventArgs e)
        {
            if (this.IsMinimized)
            {
                this.IsMinimized = false;
                this.IsDropDownOpen = false;

                template_PART_ITEMSPRESENTERPOPUP.Visibility = System.Windows.Visibility.Collapsed;
            }

            // Uncomment this if your intention is to prevent the Ribbon being maximized
            // i.e. keep it fixed in minimized mode.
            this.IsMinimized = true;
        }
    }

    //--------------------------------------------------------------------------

    // Note this routine taken from:
    // http://stackoverflow.com/questions/7034522/how-to-find-element-in-visual-tree-wp7

    public static class VisualHelper
    {
        public static T FindChild<T>(DependencyObject parent, string childName)
            where T : DependencyObject
        {
            // Confirm parent and childName are valid. 
            if (parent == null)
            {
                return null;
            }

            T foundChild = null;

            int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < childrenCount; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(parent, i);
                // If the child is not of the request child type child
                var childType = child as T;
                if (childType == null)
                {
                    // recursively drill down the tree
                    foundChild = FindChild<T>(child, childName);

                    // If the child is found, break so we do not overwrite the found child. 
                    if (foundChild != null)
                    {
                        break;
                    }
                }
                else if (!string.IsNullOrEmpty(childName))
                {
                    var frameworkElement = child as FrameworkElement;
                    // If the child's name is set for search
                    if (frameworkElement != null && frameworkElement.Name == childName)
                    {
                        // if the child's name is of the request name
                        foundChild = (T)child;
                        break;
                    }

                    // Need this in case the element we want is nested
                    // in another element of the same type
                    foundChild = FindChild<T>(child, childName);
                }
                else
                {
                    // child element found.
                    foundChild = (T)child;
                    break;
                }
            }

            return foundChild;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.