Skip to content

Commit b44391b

Browse files
feat(controls): Add TitleBar CenterContent property (#1484)
* ADD TitleBar CenterContent property to allow content in the middle of TitleBar * Update TitleBar.cs minimizing changes * bind center area VerticalAlignment to VerticalContentAlignment of TitleBar * Restored missing statements * untabify * Fix formatting in TitleBar.cs --------- Co-authored-by: pomian <13592821+pomianowski@users.noreply.github.com>
1 parent bf065a2 commit b44391b

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

src/Wpf.Ui/Controls/TitleBar/TitleBar.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public partial class TitleBar : System.Windows.Controls.Control, IThemeControl
6666
new PropertyMetadata(null)
6767
);
6868

69+
/// <summary>
70+
/// Property for <see cref="CenterContent"/>.
71+
/// </summary>
72+
public static readonly DependencyProperty CenterContentProperty = DependencyProperty.Register(
73+
nameof(CenterContent),
74+
typeof(object),
75+
typeof(TitleBar),
76+
new PropertyMetadata(null)
77+
);
78+
6979
/// <summary>
7080
/// Property for <see cref="TrailingContent"/>.
7181
/// </summary>
@@ -233,6 +243,15 @@ public object? Header
233243
set => SetValue(HeaderProperty, value);
234244
}
235245

246+
/// <summary>
247+
/// Gets or sets the content displayed in the center of the <see cref="TitleBar"/>.
248+
/// </summary>
249+
public object? CenterContent
250+
{
251+
get => GetValue(CenterContentProperty);
252+
set => SetValue(CenterContentProperty, value);
253+
}
254+
236255
/// <summary>
237256
/// Gets or sets the content displayed in right side of the <see cref="TitleBar"/>.
238257
/// </summary>
@@ -685,20 +704,15 @@ or PInvoke.WM_NCLBUTTONUP
685704

686705
if (message == PInvoke.WM_NCHITTEST)
687706
{
688-
if (TrailingContent is UIElement || Header is UIElement)
707+
if (TrailingContent is UIElement || Header is UIElement || CenterContent is UIElement)
689708
{
709+
UIElement? headerLeftUIElement = Header as UIElement;
710+
UIElement? headerCenterUIElement = CenterContent as UIElement;
690711
UIElement? headerRightUiElement = TrailingContent as UIElement;
691712

692-
if (Header is UIElement headerLeftUIElement && headerLeftUIElement != _titleBlock)
693-
{
694-
isMouseOverHeaderContent =
695-
headerLeftUIElement.IsMouseOverElement(lParam)
696-
|| (headerRightUiElement?.IsMouseOverElement(lParam) ?? false);
697-
}
698-
else
699-
{
700-
isMouseOverHeaderContent = headerRightUiElement?.IsMouseOverElement(lParam) ?? false;
701-
}
713+
isMouseOverHeaderContent = (headerLeftUIElement is not null && headerLeftUIElement != _titleBlock && headerLeftUIElement.IsMouseOverElement(lParam))
714+
|| (headerCenterUIElement?.IsMouseOverElement(lParam) ?? false)
715+
|| (headerRightUiElement?.IsMouseOverElement(lParam) ?? false);
702716
}
703717

704718
htResult = GetWindowBorderHitTestResult(hwnd, lParam);

src/Wpf.Ui/Controls/TitleBar/TitleBar.xaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
<ColumnDefinition Width="Auto" />
138138
<ColumnDefinition Width="*" />
139139
<ColumnDefinition Width="Auto" />
140+
<ColumnDefinition Width="Auto" />
140141
</Grid.ColumnDefinitions>
141142

142143
<!-- Title text or other header content -->
@@ -145,15 +146,22 @@
145146
HorizontalAlignment="Left"
146147
Content="{TemplateBinding Header}" />
147148

148-
<!-- Additional header content -->
149+
<!-- Centered content -->
149150
<ContentPresenter
150151
Grid.Column="1"
152+
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
153+
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
154+
Content="{TemplateBinding CenterContent}" />
155+
156+
<!-- Additional header content -->
157+
<ContentPresenter
158+
Grid.Column="2"
151159
HorizontalAlignment="Right"
152160
Content="{TemplateBinding TrailingContent}" />
153161

154162
<!-- Navigation buttons - Close, Restore, Maximize, Minimize -->
155163
<Grid
156-
Grid.Column="2"
164+
Grid.Column="3"
157165
HorizontalAlignment="Right"
158166
VerticalAlignment="Top">
159167
<Grid.ColumnDefinitions>

0 commit comments

Comments
 (0)