Please see the code below. Am not entirely clear on what you want, but I have covered 2 possible behavioural cases which hopefully meets your needs.
- Case 1:
If the Ribbon is set to minimized and you click on a Ribbon tab, it will turn off the minimized flag, and turn the Ribbon into a non-minimized one from that point on. You can double-click the tab, or select the minimize Ribbon from the context menu to minimize again.
- Case 2:
If you click on a Ribbon tab when the Ribbon is minimized it will keep the Ribbon in the minimized state.
I have provided 2 implementations of the code, one that just hooks a regular Ribbon, and another that creates a derived Ribbon.
<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>
Code behind of 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;
}
}
}