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