我使用Ribbon for WPF(2010 - Microsoft.Windows.Controls.Ribbon
)。
当Ribbon.IsMinimized
设置为true
时,功能区最小化。
正常行为是当我点击最小化标签时,它会暂时打开。 但有没有办法禁用它,以防止它扩展?
答案 0 :(得分:2)
请参阅下面的代码。我不清楚你想要什么,但我已经涵盖了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>
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;
}
}
}