Skip to content

Commit 8f1d02b

Browse files
authored
Merge pull request #4471 from MahApps/fix/4468_splitview_bindings
Change SplitView to ContentControl
2 parents d432fcc + 0a940b4 commit 8f1d02b

File tree

2 files changed

+82
-48
lines changed

2 files changed

+82
-48
lines changed

src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/SplitViewExamples.xaml

+9-7
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,15 @@
117117
</mah:SplitView.Pane>
118118

119119
<Grid Background="CornflowerBlue">
120-
<TextBlock Margin="20"
121-
HorizontalAlignment="Center"
122-
VerticalAlignment="Center"
123-
FontSize="20"
124-
Foreground="White"
125-
Text="This is the main content area and should be blue"
126-
TextWrapping="Wrap" />
120+
<StackPanel>
121+
<TextBlock Margin="10"
122+
HorizontalAlignment="Stretch"
123+
VerticalAlignment="Stretch"
124+
FontSize="20"
125+
Foreground="Black"
126+
Text="{Binding ElementName=sliderTest, Path=Value, Mode=OneWay, FallbackValue=0}" />
127+
<Slider x:Name="sliderTest" Margin="10" Minimum="0" Maximum="100" Value="42" />
128+
</StackPanel>
127129
</Grid>
128130

129131
</mah:SplitView>

src/MahApps.Metro/Controls/SplitView/SplitView.cs

+73-41
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections;
7+
using System.ComponentModel;
78
using System.Windows;
89
using System.Windows.Controls;
910
using System.Windows.Input;
@@ -33,7 +34,7 @@ namespace MahApps.Metro.Controls
3334
[TemplateVisualState(Name = "OpenCompactOverlayRight", GroupName = "DisplayModeStates")]
3435
[ContentProperty(nameof(Content))]
3536
[StyleTypedProperty(Property = nameof(ResizeThumbStyle), StyleTargetType = typeof(MetroThumb))]
36-
public class SplitView : Control
37+
public class SplitView : ContentControl
3738
{
3839
private Rectangle? lightDismissLayer;
3940
private RectangleGeometry? paneClipRectangle;
@@ -69,23 +70,6 @@ public double CompactPaneLength
6970
set => this.SetValue(CompactPaneLengthProperty, value);
7071
}
7172

72-
/// <summary>Identifies the <see cref="Content"/> dependency property.</summary>
73-
public static readonly DependencyProperty ContentProperty
74-
= DependencyProperty.Register(nameof(Content),
75-
typeof(UIElement),
76-
typeof(SplitView),
77-
new PropertyMetadata(null));
78-
79-
/// <summary>
80-
/// Gets or sets the contents of the main panel of a <see cref="SplitView" />.
81-
/// </summary>
82-
/// <returns>The contents of the main panel of a <see cref="SplitView" />. The default is null.</returns>
83-
public UIElement? Content
84-
{
85-
get => (UIElement?)this.GetValue(ContentProperty);
86-
set => this.SetValue(ContentProperty, value);
87-
}
88-
8973
/// <summary>Identifies the <see cref="DisplayMode"/> dependency property.</summary>
9074
public static readonly DependencyProperty DisplayModeProperty
9175
= DependencyProperty.Register(nameof(DisplayMode),
@@ -337,20 +321,87 @@ public Style? ResizeThumbStyle
337321
/// <summary>Identifies the <see cref="Pane"/> dependency property.</summary>
338322
public static readonly DependencyProperty PaneProperty
339323
= DependencyProperty.Register(nameof(Pane),
340-
typeof(UIElement),
324+
typeof(object),
341325
typeof(SplitView),
342-
new PropertyMetadata(null, UpdateLogicalChild));
326+
new FrameworkPropertyMetadata(
327+
null,
328+
new PropertyChangedCallback(OnPaneChanged)));
343329

344330
/// <summary>
345331
/// Gets or sets the contents of the pane of a <see cref="SplitView" />.
346332
/// </summary>
347333
/// <returns>The contents of the pane of a <see cref="SplitView" />. The default is null.</returns>
348-
public UIElement? Pane
334+
[Bindable(true), Category("Content")]
335+
public object? Pane
349336
{
350-
get => (UIElement?)this.GetValue(PaneProperty);
337+
get => this.GetValue(PaneProperty);
351338
set => this.SetValue(PaneProperty, value);
352339
}
353340

341+
/// <summary>
342+
/// Called when PaneProperty is invalidated
343+
/// </summary>
344+
private static void OnPaneChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
345+
{
346+
SplitView splitView = (SplitView)d;
347+
348+
splitView.OnPaneChanged(e.OldValue, e.NewValue);
349+
}
350+
351+
/// <summary>
352+
/// This method is invoked when the Header property changes.
353+
/// </summary>
354+
/// <param name="oldPane">The old value of the Header property.</param>
355+
/// <param name="newPane">The new value of the Header property.</param>
356+
protected virtual void OnPaneChanged(object oldPane, object newPane)
357+
{
358+
this.RemoveLogicalChild(oldPane);
359+
this.AddLogicalChild(newPane);
360+
361+
if (newPane is FrameworkElement frameworkElement)
362+
{
363+
frameworkElement.DataContext = this.DataContext;
364+
}
365+
}
366+
367+
/// <summary>Identifies the <see cref="PaneTemplate"/> dependency property.</summary>
368+
public static readonly DependencyProperty PaneTemplateProperty
369+
= DependencyProperty.Register(
370+
nameof(PaneTemplate),
371+
typeof(DataTemplate),
372+
typeof(SplitView),
373+
new FrameworkPropertyMetadata(
374+
null,
375+
new PropertyChangedCallback(OnPaneTemplateChanged)));
376+
377+
/// <summary>
378+
/// PaneTemplate is the template used to display the <seealso cref="Pane"/>.
379+
/// </summary>
380+
[Bindable(true), Category("Content")]
381+
public DataTemplate? PaneTemplate
382+
{
383+
get => (DataTemplate?)this.GetValue(PaneTemplateProperty);
384+
set => this.SetValue(PaneTemplateProperty, value);
385+
}
386+
387+
/// <summary>
388+
/// Called when PaneTemplateProperty is invalidated on "d."
389+
/// </summary>
390+
private static void OnPaneTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
391+
{
392+
SplitView ctrl = (SplitView)d;
393+
ctrl.OnPaneTemplateChanged((DataTemplate?)e.OldValue, (DataTemplate?)e.NewValue);
394+
}
395+
396+
/// <summary>
397+
/// This method is invoked when the PaneTemplate property changes.
398+
/// </summary>
399+
/// <param name="oldPaneTemplate">The old value of the PaneTemplate property.</param>
400+
/// <param name="newPaneTemplate">The new value of the PaneTemplate property.</param>
401+
protected virtual void OnPaneTemplateChanged(DataTemplate? oldPaneTemplate, DataTemplate? newPaneTemplate)
402+
{
403+
}
404+
354405
/// <summary>Identifies the <see cref="PaneBackground"/> dependency property.</summary>
355406
public static readonly DependencyProperty PaneBackgroundProperty
356407
= DependencyProperty.Register(nameof(PaneBackground),
@@ -504,25 +555,6 @@ private void ResizingThumb_DragDelta(object sender, System.Windows.Controls.Prim
504555
this.SetCurrentValue(OpenPaneLengthProperty, this.PanePlacement == SplitViewPanePlacement.Left ? this.OpenPaneLength + e.HorizontalChange : this.OpenPaneLength - e.HorizontalChange);
505556
}
506557

507-
private static void UpdateLogicalChild(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
508-
{
509-
if (dependencyObject is not SplitView splitView)
510-
{
511-
return;
512-
}
513-
514-
if (e.OldValue is FrameworkElement oldChild)
515-
{
516-
splitView.RemoveLogicalChild(oldChild);
517-
}
518-
519-
if (e.NewValue is FrameworkElement newChild)
520-
{
521-
splitView.AddLogicalChild(newChild);
522-
newChild.DataContext = splitView.DataContext;
523-
}
524-
}
525-
526558
/// <inheritdoc />
527559
protected override IEnumerator LogicalChildren
528560
{

0 commit comments

Comments
 (0)