Skip to content

Commit

Permalink
Fixes #315 by changing the way logical children are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed May 27, 2016
1 parent 4db55ab commit 3828f35
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 74 deletions.
16 changes: 1 addition & 15 deletions Fluent.Ribbon/Controls/Backstage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,7 @@ public UIElement Content
/// This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register(nameof(Content), typeof(UIElement), typeof(Backstage), new UIPropertyMetadata(null, OnContentChanged));

private static void OnContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var backstage = (Backstage)d;
if (e.OldValue != null)
{
backstage.RemoveLogicalChild(e.OldValue);
}

if (e.NewValue != null)
{
backstage.AddLogicalChild(e.NewValue);
}
}
DependencyProperty.Register(nameof(Content), typeof(UIElement), typeof(Backstage), new UIPropertyMetadata(null));

#endregion

Expand Down
37 changes: 11 additions & 26 deletions Fluent.Ribbon/Controls/DropDownButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,27 +152,7 @@ public object Icon
/// <summary>
/// Using a DependencyProperty as the backing store for Icon. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty IconProperty = RibbonControl.IconProperty.AddOwner(typeof(DropDownButton), new UIPropertyMetadata(null, OnIconChanged));

private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = (DropDownButton)d;

var oldElement = e.OldValue as FrameworkElement;

if (oldElement != null)
{
element.RemoveLogicalChild(oldElement);
}

var newElement = e.NewValue as FrameworkElement;

if (newElement != null
&& LogicalTreeHelper.GetParent(newElement) == null)
{
element.AddLogicalChild(newElement);
}
}
public static readonly DependencyProperty IconProperty = RibbonControl.IconProperty.AddOwner(typeof(DropDownButton), new UIPropertyMetadata(null));

#endregion

Expand Down Expand Up @@ -732,7 +712,7 @@ private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyProper

control.Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
(DispatcherOperationCallback)delegate(object arg)
(DispatcherOperationCallback)delegate (object arg)
{
var ctrl = (DropDownButton)arg;

Expand Down Expand Up @@ -798,9 +778,9 @@ private void OnDropDownOpened()
public virtual FrameworkElement CreateQuickAccessItem()
{
var button = new DropDownButton
{
Size = RibbonControlSize.Small
};
{
Size = RibbonControlSize.Small
};

this.BindQuickAccessItem(button);
RibbonControl.Bind(this, button, "DisplayMemberPath", DisplayMemberPathProperty, BindingMode.OneWay);
Expand All @@ -825,7 +805,7 @@ public virtual FrameworkElement CreateQuickAccessItem()
/// <param name="e"></param>
protected void OnQuickAccessOpened(object sender, EventArgs e)
{
var buttonInQuickAccess = (DropDownButton)sender;
var buttonInQuickAccess = (DropDownButton)sender;

buttonInQuickAccess.DropDownClosed += this.OnQuickAccessMenuClosedOrUnloaded;
buttonInQuickAccess.Unloaded += this.OnQuickAccessMenuClosedOrUnloaded;
Expand Down Expand Up @@ -902,6 +882,11 @@ protected override IEnumerator LogicalChildren
yield return this.Icon;
}

if (this.LargeIcon != null)
{
yield return this.LargeIcon;
}

foreach (var item in this.Items)
{
yield return item;
Expand Down
36 changes: 3 additions & 33 deletions Fluent.Ribbon/Controls/Ribbon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ public UIElement Menu
/// This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty MenuProperty =
DependencyProperty.Register(nameof(Menu), typeof(UIElement), typeof(Ribbon), new UIPropertyMetadata(null, AddOrRemoveLogicalChildOnPropertyChanged));
DependencyProperty.Register(nameof(Menu), typeof(UIElement), typeof(Ribbon), new UIPropertyMetadata(null));

#endregion

Expand All @@ -461,7 +461,7 @@ public StartScreen StartScreen
/// <see cref="DependencyProperty"/> for <see cref="StartScreen"/>
/// </summary>
public static readonly DependencyProperty StartScreenProperty =
DependencyProperty.Register(nameof(StartScreen), typeof(StartScreen), typeof(Ribbon), new UIPropertyMetadata(null, AddOrRemoveLogicalChildOnPropertyChanged));
DependencyProperty.Register(nameof(StartScreen), typeof(StartScreen), typeof(Ribbon), new UIPropertyMetadata(null));

/// <summary>
/// Window title
Expand Down Expand Up @@ -557,20 +557,6 @@ private static void OnSelectedTabIndexChanged(DependencyObject d, DependencyProp
}
}

private static void AddOrRemoveLogicalChildOnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ribbon = (Ribbon)d;
if (e.OldValue != null)
{
ribbon.RemoveLogicalChild(e.OldValue);
}

if (e.NewValue != null)
{
ribbon.AddLogicalChild(e.NewValue);
}
}

/// <summary>
/// Gets the first visible TabItem
/// </summary>
Expand Down Expand Up @@ -849,8 +835,7 @@ protected override IEnumerator LogicalChildren
yield return this.QuickAccessToolBar;
}

if (this.TabControl != null
&& this.TabControl.ToolbarPanel != null)
if (this.TabControl?.ToolbarPanel != null)
{
yield return this.TabControl.ToolbarPanel;
}
Expand Down Expand Up @@ -1455,18 +1440,8 @@ protected override void OnGotFocus(RoutedEventArgs e)
[SuppressMessage("Microsoft.Maintainability", "CA1502")]
public override void OnApplyTemplate()
{
if (this.layoutRoot != null)
{
this.RemoveLogicalChild(this.layoutRoot);
}

this.layoutRoot = this.GetTemplateChild("PART_LayoutRoot") as Panel;

if (this.layoutRoot != null)
{
this.AddLogicalChild(this.layoutRoot);
}

if (this.TitleBar != null)
{
foreach (var ribbonContextualTabGroup in this.ContextualGroups)
Expand Down Expand Up @@ -1573,11 +1548,6 @@ public override void OnApplyTemplate()
};
this.QuickAccessToolBar.SetBinding(QuickAccessToolBar.CanQuickAccessLocationChangingProperty, binding);

if (this.QuickAccessToolBar.Parent == null)
{
this.AddLogicalChild(this.QuickAccessToolBar);
}

this.QuickAccessToolBar.Loaded += this.OnFirstToolbarLoaded;
}
}
Expand Down

0 comments on commit 3828f35

Please sign in to comment.