Skip to content

Commit

Permalink
Handling of logical children is very weird in WPF...
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed May 27, 2016
1 parent 4b3aa44 commit 9733f78
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions Fluent.Ribbon.Showcase/TestWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
x:Name="window"
x:Class="FluentTest.TestWindow"
Title="{Binding DataContext.Title, ElementName=TestContent}"
FlowDirection="LeftToRight"
Height="768"
Width="1024"
MinWidth="80"
Expand Down
24 changes: 19 additions & 5 deletions Fluent.Ribbon/Controls/Backstage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,29 @@ 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));
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);
}
}

#endregion

#region LogicalChildren
#region LogicalChildren

/// <summary>
/// Gets an enumerator for logical child elements of this element.
/// </summary>
/// <summary>
/// Gets an enumerator for logical child elements of this element.
/// </summary>
protected override IEnumerator LogicalChildren
{
get
Expand Down
25 changes: 17 additions & 8 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));
DependencyProperty.Register(nameof(Menu), typeof(UIElement), typeof(Ribbon), new UIPropertyMetadata(null, AddOrRemoveLogicalChildOnPropertyChanged));

#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));
DependencyProperty.Register(nameof(StartScreen), typeof(StartScreen), typeof(Ribbon), new UIPropertyMetadata(null, AddOrRemoveLogicalChildOnPropertyChanged));

/// <summary>
/// Window title
Expand Down Expand Up @@ -498,7 +498,7 @@ public RibbonTabItem SelectedTabItem
/// Using a DependencyProperty as the backing store for SelectedTabItem. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty SelectedTabItemProperty =
DependencyProperty.Register("SelectedTabItem", typeof(RibbonTabItem), typeof(Ribbon), new UIPropertyMetadata(null, OnSelectedTabItemChanged));
DependencyProperty.Register(nameof(SelectedTabItem), typeof(RibbonTabItem), typeof(Ribbon), new UIPropertyMetadata(null, OnSelectedTabItemChanged));

private static void OnSelectedTabItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
Expand Down Expand Up @@ -557,6 +557,20 @@ 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 @@ -813,11 +827,6 @@ protected override IEnumerator LogicalChildren
yield return this.layoutRoot;
}

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

if (this.StartScreen != null)
{
yield return this.StartScreen;
Expand Down

0 comments on commit 9733f78

Please sign in to comment.