Skip to content

Commit 17cb390

Browse files
authored
Merge pull request #3225 from MahApps/feature/GH-3192_fix_setting_ShowTitleBar
(GH-3192) Fix setting ShowTitleBar
2 parents 9537f98 + 66d1288 commit 17cb390

File tree

4 files changed

+30
-56
lines changed

4 files changed

+30
-56
lines changed

src/MahApps.Metro.Samples/MahApps.Metro.Demo/MainWindow.xaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
Closing="MetroWindow_Closing"
1919
GlowBrush="{DynamicResource AccentColorBrush}"
2020
Icon="mahapps.metro.logo2.ico"
21-
LeftWindowCommandsOverlayBehavior="Never"
2221
NonActiveGlowBrush="#CDFF0000"
23-
RightWindowCommandsOverlayBehavior="Never"
2422
ShowIconOnTitleBar="True"
25-
ShowTitleBar="{Binding ShowMyTitleBar, Mode=OneWay}"
23+
ShowTitleBar="{Binding ShowMyTitleBar, Mode=TwoWay}"
2624
WindowStartupLocation="CenterScreen"
2725
mc:Ignorable="d">
2826
<!--
@@ -188,7 +186,7 @@
188186
<MenuItem Header="Window">
189187
<MenuItem Header="ShowTitleBar"
190188
IsCheckable="True"
191-
IsChecked="{Binding Path=ShowMyTitleBar, UpdateSourceTrigger=PropertyChanged}" />
189+
IsChecked="{Binding Path=ShowMyTitleBar, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
192190
<MenuItem Header="ShowInTaskbar"
193191
IsCheckable="True"
194192
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}, Path=ShowInTaskbar}" />

src/MahApps.Metro/Controls/MetroWindow.cs

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public class MetroWindow : Window
124124
public static readonly DependencyProperty RightWindowCommandsProperty = DependencyProperty.Register("RightWindowCommands", typeof(WindowCommands), typeof(MetroWindow), new PropertyMetadata(null, UpdateLogicalChilds));
125125
public static readonly DependencyProperty WindowButtonCommandsProperty = DependencyProperty.Register("WindowButtonCommands", typeof(WindowButtonCommands), typeof(MetroWindow), new PropertyMetadata(null, UpdateLogicalChilds));
126126

127-
public static readonly DependencyProperty LeftWindowCommandsOverlayBehaviorProperty = DependencyProperty.Register("LeftWindowCommandsOverlayBehavior", typeof(WindowCommandsOverlayBehavior), typeof(MetroWindow), new PropertyMetadata(WindowCommandsOverlayBehavior.Always));
128-
public static readonly DependencyProperty RightWindowCommandsOverlayBehaviorProperty = DependencyProperty.Register("RightWindowCommandsOverlayBehavior", typeof(WindowCommandsOverlayBehavior), typeof(MetroWindow), new PropertyMetadata(WindowCommandsOverlayBehavior.Always));
127+
public static readonly DependencyProperty LeftWindowCommandsOverlayBehaviorProperty = DependencyProperty.Register("LeftWindowCommandsOverlayBehavior", typeof(WindowCommandsOverlayBehavior), typeof(MetroWindow), new PropertyMetadata(WindowCommandsOverlayBehavior.Flyouts));
128+
public static readonly DependencyProperty RightWindowCommandsOverlayBehaviorProperty = DependencyProperty.Register("RightWindowCommandsOverlayBehavior", typeof(WindowCommandsOverlayBehavior), typeof(MetroWindow), new PropertyMetadata(WindowCommandsOverlayBehavior.Flyouts));
129129
public static readonly DependencyProperty WindowButtonCommandsOverlayBehaviorProperty = DependencyProperty.Register("WindowButtonCommandsOverlayBehavior", typeof(WindowCommandsOverlayBehavior), typeof(MetroWindow), new PropertyMetadata(WindowCommandsOverlayBehavior.Always));
130130
public static readonly DependencyProperty IconOverlayBehaviorProperty = DependencyProperty.Register("IconOverlayBehavior", typeof(WindowCommandsOverlayBehavior), typeof(MetroWindow), new PropertyMetadata(WindowCommandsOverlayBehavior.Never));
131131

@@ -474,7 +474,7 @@ private static void OnShowTitleBarPropertyChangedCallback(DependencyObject d, De
474474
var window = (MetroWindow)d;
475475
if (e.NewValue != e.OldValue)
476476
{
477-
window.SetVisibiltyForAllTitleElements((bool)e.NewValue);
477+
window.SetVisibiltyForAllTitleElements();
478478
}
479479
}
480480

@@ -503,29 +503,12 @@ private static void OnUseNoneWindowStylePropertyChangedCallback(DependencyObject
503503
{
504504
// if UseNoneWindowStyle = true no title bar should be shown
505505
var useNoneWindowStyle = (bool)e.NewValue;
506-
var window = (MetroWindow)d;
507-
window.ToggleNoneWindowStyle(useNoneWindowStyle, window.ShowTitleBar);
508-
}
509-
}
510506

511-
private void ToggleNoneWindowStyle(bool useNoneWindowStyle, bool isTitleBarVisible)
512-
{
513-
// UseNoneWindowStyle means no title bar, window commands or min, max, close buttons
514-
if (useNoneWindowStyle)
515-
{
516-
this.SetCurrentValue(ShowTitleBarProperty, false);
517-
}
518-
else
519-
{
520-
this.SetCurrentValue(ShowTitleBarProperty, isTitleBarVisible);
521-
}
522-
if (LeftWindowCommandsPresenter != null)
523-
{
524-
LeftWindowCommandsPresenter.Visibility = useNoneWindowStyle ? Visibility.Collapsed : Visibility.Visible;
525-
}
526-
if (RightWindowCommandsPresenter != null)
527-
{
528-
RightWindowCommandsPresenter.Visibility = useNoneWindowStyle ? Visibility.Collapsed : Visibility.Visible;
507+
// UseNoneWindowStyle means no title bar, window commands or min, max, close buttons
508+
if (useNoneWindowStyle)
509+
{
510+
((MetroWindow)d).SetCurrentValue(ShowTitleBarProperty, false);
511+
}
529512
}
530513
}
531514

@@ -615,7 +598,7 @@ private static void TitlebarHeightPropertyChangedCallback(DependencyObject depen
615598
var window = (MetroWindow)dependencyObject;
616599
if (e.NewValue != e.OldValue)
617600
{
618-
window.SetVisibiltyForAllTitleElements((int)e.NewValue > 0);
601+
window.SetVisibiltyForAllTitleElements();
619602
}
620603
}
621604

@@ -630,24 +613,24 @@ private void SetVisibiltyForIcon()
630613
}
631614
}
632615

633-
private void SetVisibiltyForAllTitleElements(bool visible)
616+
private void SetVisibiltyForAllTitleElements()
634617
{
635618
this.SetVisibiltyForIcon();
636-
var newVisibility = visible && this.ShowTitleBar ? Visibility.Visible : Visibility.Collapsed;
619+
var newVisibility = this.TitlebarHeight > 0 && this.ShowTitleBar && !this.UseNoneWindowStyle ? Visibility.Visible : Visibility.Collapsed;
637620

638621
this.titleBar?.SetCurrentValue(VisibilityProperty, newVisibility);
639622
this.titleBarBackground?.SetCurrentValue(VisibilityProperty, newVisibility);
640623

641-
newVisibility = this.LeftWindowCommandsOverlayBehavior.HasFlag(WindowCommandsOverlayBehavior.HiddenTitleBar) ? Visibility.Visible : newVisibility;
624+
newVisibility = this.LeftWindowCommandsOverlayBehavior.HasFlag(WindowCommandsOverlayBehavior.HiddenTitleBar) && !this.UseNoneWindowStyle ? Visibility.Visible : newVisibility;
642625
this.LeftWindowCommandsPresenter?.SetCurrentValue(VisibilityProperty, newVisibility);
643626

644-
newVisibility = this.RightWindowCommandsOverlayBehavior.HasFlag(WindowCommandsOverlayBehavior.HiddenTitleBar) ? Visibility.Visible : newVisibility;
627+
newVisibility = this.RightWindowCommandsOverlayBehavior.HasFlag(WindowCommandsOverlayBehavior.HiddenTitleBar) && !this.UseNoneWindowStyle ? Visibility.Visible : newVisibility;
645628
this.RightWindowCommandsPresenter?.SetCurrentValue(VisibilityProperty, newVisibility);
646629

647630
newVisibility = this.WindowButtonCommandsOverlayBehavior.HasFlag(WindowCommandsOverlayBehavior.HiddenTitleBar) ? Visibility.Visible : newVisibility;
648631
this.WindowButtonCommandsPresenter?.SetCurrentValue(VisibilityProperty, newVisibility);
649632

650-
SetWindowEvents();
633+
this.SetWindowEvents();
651634
}
652635

653636
/// <summary>
@@ -1044,8 +1027,6 @@ private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
10441027
VisualStateManager.GoToState(this, "AfterLoaded", true);
10451028
}
10461029

1047-
this.ToggleNoneWindowStyle(this.UseNoneWindowStyle, this.ShowTitleBar);
1048-
10491030
if (this.Flyouts == null)
10501031
{
10511032
this.Flyouts = new FlyoutsControl();
@@ -1243,7 +1224,7 @@ public override void OnApplyTemplate()
12431224
this.windowTitleThumb = GetTemplateChild(PART_WindowTitleThumb) as Thumb;
12441225
this.flyoutModalDragMoveThumb = GetTemplateChild(PART_FlyoutModalDragMoveThumb) as Thumb;
12451226

1246-
this.SetVisibiltyForAllTitleElements(this.TitlebarHeight > 0);
1227+
this.SetVisibiltyForAllTitleElements();
12471228

12481229
var metroContentControl = GetTemplateChild(PART_Content) as MetroContentControl;
12491230
if (metroContentControl != null)

src/MahApps.Metro/Themes/MetroWindow.xaml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
Grid.ColumnSpan="5"
4747
Fill="{TemplateBinding WindowTitleBrush}"
4848
Focusable="False"
49-
StrokeThickness="0"
50-
Visibility="{TemplateBinding ShowTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}" />
49+
StrokeThickness="0" />
5150

5251
<!-- icon -->
5352
<ContentControl x:Name="PART_Icon"
@@ -71,8 +70,7 @@
7170
VerticalAlignment="Top"
7271
Panel.ZIndex="1"
7372
Content="{Binding LeftWindowCommands, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
74-
Focusable="False"
75-
Visibility="{TemplateBinding ShowTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}" />
73+
Focusable="False" />
7674

7775
<Controls:MetroThumb x:Name="PART_WindowTitleThumb"
7876
Grid.Row="1"
@@ -91,8 +89,7 @@
9189
Content="{TemplateBinding Title}"
9290
ContentCharacterCasing="{TemplateBinding TitleCharacterCasing}"
9391
ContentTemplate="{TemplateBinding TitleTemplate}"
94-
Focusable="False"
95-
Visibility="{TemplateBinding ShowTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}">
92+
Focusable="False">
9693
<ContentControl.Foreground>
9794
<MultiBinding Converter="{x:Static Converters:BackgroundToForegroundConverter.Instance}">
9895
<Binding ElementName="PART_WindowTitleBackground"
@@ -113,8 +110,7 @@
113110
VerticalAlignment="Top"
114111
Panel.ZIndex="1"
115112
Content="{Binding RightWindowCommands, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
116-
Focusable="False"
117-
Visibility="{TemplateBinding ShowTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}" />
113+
Focusable="False" />
118114

119115
<!-- the window button commands -->
120116
<ContentPresenter x:Name="PART_WindowButtonCommands"
@@ -306,8 +302,7 @@
306302
Grid.ColumnSpan="5"
307303
Fill="{TemplateBinding WindowTitleBrush}"
308304
Focusable="False"
309-
StrokeThickness="0"
310-
Visibility="{TemplateBinding ShowTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}" />
305+
StrokeThickness="0" />
311306

312307
<!-- icon -->
313308
<ContentControl x:Name="PART_Icon"
@@ -331,8 +326,7 @@
331326
VerticalAlignment="Top"
332327
Panel.ZIndex="1"
333328
Content="{Binding LeftWindowCommands, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
334-
Focusable="False"
335-
Visibility="{TemplateBinding ShowTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}" />
329+
Focusable="False" />
336330

337331
<Controls:MetroThumb x:Name="PART_WindowTitleThumb"
338332
Grid.Row="1"
@@ -352,8 +346,7 @@
352346
Content="{TemplateBinding Title}"
353347
ContentCharacterCasing="{TemplateBinding TitleCharacterCasing}"
354348
ContentTemplate="{TemplateBinding TitleTemplate}"
355-
Focusable="False"
356-
Visibility="{TemplateBinding ShowTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}">
349+
Focusable="False">
357350
<ContentControl.Foreground>
358351
<MultiBinding Converter="{x:Static Converters:BackgroundToForegroundConverter.Instance}">
359352
<Binding ElementName="PART_WindowTitleBackground"
@@ -374,8 +367,7 @@
374367
VerticalAlignment="Top"
375368
Panel.ZIndex="1"
376369
Content="{Binding RightWindowCommands, RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}"
377-
Focusable="False"
378-
Visibility="{TemplateBinding ShowTitleBar, Converter={StaticResource BooleanToVisibilityConverter}}" />
370+
Focusable="False" />
379371

380372
<!-- the window button commands -->
381373
<ContentPresenter x:Name="PART_WindowButtonCommands"

src/Mahapps.Metro.Tests/Tests/MetroWindowTest.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ public async Task WindowCommandsShouldHaveTheParentWindow()
3434

3535
[Fact]
3636
[DisplayTestMethodName]
37-
public async Task ShowsRightWindowCommandsOnTopByDefault()
37+
public async Task CheckDefaultOverlayBehaviors()
3838
{
3939
await TestHost.SwitchToAppThread();
4040

4141
var window = await WindowHelpers.CreateInvisibleWindowAsync<MetroWindow>();
4242

43-
Assert.Equal(WindowCommandsOverlayBehavior.Always, window.RightWindowCommandsOverlayBehavior);
43+
Assert.Equal(WindowCommandsOverlayBehavior.Never, window.IconOverlayBehavior);
44+
Assert.Equal(WindowCommandsOverlayBehavior.Flyouts, window.LeftWindowCommandsOverlayBehavior);
45+
Assert.Equal(WindowCommandsOverlayBehavior.Flyouts, window.RightWindowCommandsOverlayBehavior);
46+
Assert.Equal(WindowCommandsOverlayBehavior.Always, window.WindowButtonCommandsOverlayBehavior);
4447
}
4548

4649
[Fact]

0 commit comments

Comments
 (0)