Skip to content

Conversation

@luca-domenichini
Copy link

@luca-domenichini luca-domenichini commented Jul 11, 2025

This PR is fix for #1483
It adds a CenterContent DependencyProperty and a new column in the Grid of TitleBar, making developer able to put content in the center area of the titlebar.

Moreover, with HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" we make developer able to align the centered area more precisely, to the left, center or right edge.

Pull request type

Please check the type of change your PR introduces:

  • Update
  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes

What is the current behavior?

Currently there are few, less obvious alternatives.

Issue Number: #1483

What is the new behavior?

Devs are now able to put content directly in the centered area of the title bar.

@github-actions github-actions bot added controls Changes to the appearance or logic of custom controls. styles Topic is related to styles PR Pull request dotnet release titlebar Titlebar updates labels Jul 11, 2025
@github-actions github-actions bot added the ⭐ top pull request Top pull request. label Jul 24, 2025
@pomianowski pomianowski linked an issue Nov 7, 2025 that may be closed by this pull request
@pomianowski pomianowski changed the title ADD TitleBar CenterContent property, fix for #1483 feat(controls): Add TitleBar CenterContent property Nov 7, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a CenterContent property to the TitleBar control, enabling developers to place content in the center area of the title bar.

Key changes:

  • New CenterContent dependency property with getter/setter
  • Updated XAML template with additional grid column for centered content
  • Enhanced mouse hit-testing logic to handle center content interactions

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Wpf.Ui/Controls/TitleBar/TitleBar.cs Added CenterContent dependency property and updated window procedure hook to handle mouse-over detection for center content
src/Wpf.Ui/Controls/TitleBar/TitleBar.xaml Added new grid column and ContentPresenter for center content, reordered existing columns

Comment on lines 705 to 719
if (message == PInvoke.WM_NCHITTEST)
{
if (TrailingContent is UIElement || Header is UIElement || CenterContent is UIElement)
{
UIElement? headerLeftUIElement = Header as UIElement;
UIElement? headerCenterUIElement = CenterContent as UIElement;
UIElement? headerRightUiElement = TrailingContent as UIElement;

var e = new HwndProcEventArgs(hwnd, msg, wParam, lParam, isMouseOverHeaderContent);
WndProcInvoked?.Invoke(this, e);
isMouseOverHeaderContent = (headerLeftUIElement is not null && headerLeftUIElement != _titleBlock && headerLeftUIElement.IsMouseOverElement(lParam))
|| (headerCenterUIElement?.IsMouseOverElement(lParam) ?? false)
|| (headerRightUiElement?.IsMouseOverElement(lParam) ?? false);
}

if (e.ReturnValue != null)
{
handled = e.Handled;
return e.ReturnValue ?? IntPtr.Zero;
}
htResult = GetWindowBorderHitTestResult(hwnd, lParam);
}
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tabs are used for indentation instead of spaces. According to the project's .editorconfig, C# files should use spaces (indent_style = space). Please replace tabs with spaces for consistent formatting.

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

switch (message)
switch (message)
Copy link

Copilot AI Nov 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tab used for indentation instead of spaces. According to the project's .editorconfig, C# files should use spaces (indent_style = space). Please replace the tab with spaces.

Suggested change
switch (message)
switch (message)

Copilot uses AI. Check for mistakes.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

@luca-domenichini
Copy link
Author

Hi @pomianowski , is there something more I can do to make this PR go ahead?
Maybe I could ask for @Nuklon review, since I see he is probably the most active dev on this repo?

@apachezy
Copy link
Contributor

I had also used the same approach to allow user content in the center of the title bar, but I realized it might be a temporary workaround and didn't submit a PR.

Later, I found that changing the HorizontalAlignment of the TrailingContent to Stretch would allow users to freely place content (left, center, or right), because:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" /> <!-- Title text or other header content -->
    <ColumnDefinition Width="*" /> <!-- TrailingContent!!!!!!!!!!! -->
    <ColumnDefinition Width="Auto" /> <!-- System buttons -->
</Grid.ColumnDefinitions>

The column where TrailingContent resides has a width of "*", but its HorizontalAlignment was set to Right. This is not reasonable — even though the remaining space is fully available, users were forced to align their content to the "right".

Changing it to Stretch is both logical and user-friendly, as it allows content to utilize the full available space. Why not do it?

Moreover, this method requires minimal changes, especially since no modifications are needed in the HwndSourceHook. This also means minimal burden for reviewers.

Back then, I wasn't very familiar with the PR process. I once accidentally included this change in a PR, but it was rejected — haha!

@luca-domenichini
Copy link
Author

I think your arguments are valuable. Thanks for adding your point of view ⭐ Let's hope for a review by the maintainers! ⭐

@luca-domenichini
Copy link
Author

@apachezy thumb up the PR if you want to push it further 👍

@apachezy
Copy link
Contributor

So I suggest adopting the "TrailingContent.HorizontalAlignment = Stretch" approach, mark the TrailingContent property as obsolete, introduce a new property (e.g., "RemainingSpaceContent") to replace it, and then internally link TrailingContent to this new property.

This way, the changes required are minimal. It avoids cluttering TitleBar with too many "Content" properties, while still meeting the requirements. Why not go for it?

@apachezy
Copy link
Contributor

I'll wait for your new solution to be implemented, and I'll give you a ton of likes for it! 👍

@luca-domenichini
Copy link
Author

hmm.. I think having just a new center property will not break existing applications. Changing the existing one as you suggest, also could break them.
My PR just add new content where previously there was nothing, so existing stuff should still work.
If we change the existing to Stretch I fear many users would be affected.. am I wrong?

@apachezy
Copy link
Contributor

Hmm... I think this is just part of the natural process of software iteration. Changing the TrailingContent.HorizontalAlignment property would only have the effect of making content that was originally right-aligned appear "distributed". Once developers notice this, they'll realize: "Haha, now I can place content anywhere I want!"

@luca-domenichini
Copy link
Author

I see your point, for a brand new library would make more sense, and yes, for me would not be a problem since I'm making the PR and I would naturally adapt my code as well.

Though, I think the kind of change you are suggesting should be approved by maintainers more carefully, and since I want this PR to go fast without breaking anyone.. well.. let's wait for @pomianowski to react to your points.

Repeating myself: for me would be OK with your proposal. Just curious about owner directions.

@apachezy
Copy link
Contributor

I feel that there are still some unresolved concerns in the current TitleBar implementation, particularly regarding the combination of WPF techniques and the Windows message–based approach.

While it seems to be working correctly right now, this is largely thanks to the accumulated efforts of multiple contributors.

I expect that more focused refinements will be needed in the future, so at this stage, I think it makes sense to achieve the required functionality with minimal changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

controls Changes to the appearance or logic of custom controls. dotnet PR Pull request release ⭐ top pull request Top pull request. styles Topic is related to styles titlebar Titlebar updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TitleBar - ADD support for a centered content area

2 participants