【发布时间】:2011-04-23 09:59:05
【问题描述】:
我正在使用 prism 将视图加载到区域。问题是加载的视图与主窗口的标题栏重叠 - 该栏包含标题、关闭/最小化/最大化按钮。如何获取标题栏的高度?更喜欢在 xaml 代码中正确使用它。
【问题讨论】:
我正在使用 prism 将视图加载到区域。问题是加载的视图与主窗口的标题栏重叠 - 该栏包含标题、关闭/最小化/最大化按钮。如何获取标题栏的高度?更喜欢在 xaml 代码中正确使用它。
【问题讨论】:
过了一会儿,我想通了:
<Window xmlns:local="clr-namespace:System.Windows;assembly=PresentationFramework">
<YourView Height="{x:Static local:SystemParameters.WindowCaptionHeight}" />
</Window>
希望有帮助!
【讨论】:
System.Windows.SystemParameters.WindowCaptionHeight 返回 23 与我通过调试器验证的 39。我的 XAML 的根元素为 Window,其中 Margin、BorderThickness 和 Padding 为 0,其 Content 元素为 DockPanel,Margin 为 0。 DockPanel 的 ActualHeight 是 39 Window 的。
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();
}
}
【讨论】: