【问题标题】:How to get the height of the title bar of the main application windows?如何获取主应用程序窗口标题栏的高度?
【发布时间】:2011-04-23 09:59:05
【问题描述】:

我正在使用 prism 将视图加载到区域。问题是加载的视图与主窗口的标题栏重叠 - 该栏包含标题、关闭/最小化/最大化按钮。如何获取标题栏的高度?更喜欢在 xaml 代码中正确使用它。

【问题讨论】:

    标签: wpf xaml height


    【解决方案1】:

    过了一会儿,我想通了:

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

    希望有帮助!

    【讨论】:

    • 在我的 Intel Core i7-6700 CPU@ 3.40GHz 上运行 Windows 10 Pro 64 位(版本 10.0.15063)和 Visual Studio 2015 Enterprise(版本 14.0.25431.01 更新 3),面向 .NET Framework 4.5 .2 有约 16 GB RAM 和约 110 GB HD 免费,System.Windows.SystemParameters.WindowCaptionHeight 返回 23 与我通过调试器验证的 39。我的 XAML 的根元素为 Window,其中 MarginBorderThicknessPadding 为 0,其 Content 元素为 DockPanelMargin 为 0。 DockPanelActualHeight 是 39 Window 的。
    • 这里完全一样。
    • 请改用this
    【解决方案2】:

    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>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-25
      • 2015-04-15
      • 1970-01-01
      • 2011-05-15
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      相关资源
      最近更新 更多