Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the height of the title bar of the main application windows?

Tags:

height

wpf

xaml

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.

like image 347
Nam G VU Avatar asked Oct 10 '10 08:10

Nam G VU


2 Answers

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!

like image 175
Nam G VU Avatar answered Sep 28 '22 06:09

Nam G VU


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();
    }
}
like image 20
l33t Avatar answered Sep 28 '22 06:09

l33t