I'm using prism to load views to region. The problem is the loaded view overlapped the title bar of the main windows - the bar contains caption, close/minimize/maximize buttons. How can I get the title bar's height? Prefer to get it right in the xaml codes.
How to get the height of the title bar of the main application windows?
After a while, I figure it out:
<Window xmlns:local="clr-namespace:System.Windows;assembly=PresentationFramework">
<YourView Height="{x:Static local:SystemParameters.WindowCaptionHeight}" />
</Window>
Hope that helps!
Exactly the same here. –
Commerce
Please try this instead! –
Conservatory
SystemParameters.WindowCaptionHeight
is in pixels whereas WPF
needs screen cordinates. You have to convert it!
<Grid>
<Grid.Resources>
<wpfApp1:Pixel2ScreenConverter x:Key="Pixel2ScreenConverter" />
</Grid.Resources>
<YourView Height="{Binding Source={x:Static SystemParameters.WindowCaptionHeight},Converter={StaticResource Pixel2ScreenConverter}}" />
</Grid>
aa
public class Pixel2ScreenConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double pixels = (double) value;
bool horizontal = Equals(parameter, true);
double points = 0d;
// NOTE: Ideally, we would get the source from a visual:
// source = PresentationSource.FromVisual(visual);
//
using (var source = new HwndSource(new HwndSourceParameters()))
{
var matrix = source.CompositionTarget?.TransformToDevice;
if (matrix.HasValue)
{
points = pixels * (horizontal ? matrix.Value.M11 : matrix.Value.M22);
}
}
return points;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
I think you can do this since .NET Framework 4.5.
Here is what you can do:
double title_height = (new WindowChrome()).CaptionHeight;
© 2022 - 2024 — McMap. All rights reserved.
System.Windows.SystemParameters.WindowCaptionHeight
returned 23 vs. the 39 that I verified via the Debugger. My XAML has as its root Element aWindow
with 0's forMargin
,BorderThickness
andPadding
and itsContent
Element is aDockPanel
with 0's forMargin
. TheDockPanel
'sActualHeight
was 39 < than theWindow
's. – Sarchet