如何获取主应用程序窗口标题栏的高度?

时间:2010-10-10 08:59:38

标签: wpf xaml height

我正在使用棱镜将视图加载到区域。问题是加载的视图重叠了主窗口的标题栏 - 栏包含标题,关闭/最小化/最大化按钮。如何获得标题栏的高度?希望在xaml代码中正确使用它。

2 个答案:

答案 0 :(得分:15)

过了一会儿,我弄清楚了:

<Window xmlns:local="clr-namespace:System.Windows;assembly=PresentationFramework">
  <YourView Height="{x:Static local:SystemParameters.WindowCaptionHeight}" />
</Window>

希望有所帮助!

答案 1 :(得分:0)

SystemParameters.WindowCaptionHeight以像素为单位,而WPF需要屏幕坐标。您必须将其转换!

<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();
    }
}