gpt4 book ai didi

c# - 与 WindowChrome 一起使用时,窗口模板中的边框边距没有任何效果

转载 作者:可可西里 更新时间:2023-11-01 08:25:58 30 4
gpt4 key购买 nike

我正在检查 System.Windows.Shell 库 (v 3.5.41019.1) 中的 WindowChrome 类。当我尝试创建 Window 模板时,模板中 Border 元素的边距似乎没有效果:

<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="clr-namespace:Microsoft.Windows.Shell;assembly=Microsoft.Windows.Shell"
Title="MainWindow" Height="350" Width="525" Style="{DynamicResource WindowStyle1}">
<Window.Resources>
<Style x:Key="WindowStyle1" TargetType="{x:Type Window}">
<!-- Here is the WindowChrome.-->
<Setter Property="shell:WindowChrome.WindowChrome">
<Setter.Value>
<shell:WindowChrome />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<!-- And here is the Border. Its margin has no effect as far as I can tell.-->
<Border Margin="25" Background="Red">
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>

</Grid>
</Window>

你认为这是什么原因?我想知道,因为我看到有些人使用类似 *:

<Border x:Name="WindowBorder" Margin="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowNonClientFrameThickness}" Background="Red">

但由于它对我的测试没有任何影响,这样做的意义何在?

(*) 使用它的地方之一是 ModernUI项目 CodePlex .

编辑:我已经在打开 Aero 的 Windows 7 上对此进行了测试。

编辑 2:Aero 关闭时仍然是一样的。

最佳答案

根据 MSDN,WindowChrome

Represents an object that describes the customizations to the non-client area of a window.

阅读 MSDN 示例并播放您的代码一段时间后,我注意到您的代码应该类似于 MSDN 示例代码中的以下内容:

<Style x:Key="StandardStyle" TargetType="{x:Type local:MainWindow}">
<Setter Property="shell:WindowChrome.WindowChrome">
<Setter.Value>
<shell:WindowChrome />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MainWindow}">
<!--Note there is a Grid as the root-->
<Grid>
<Border Background="White"
Margin="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=WindowNonClientFrameThickness}">
<ContentPresenter Content="{TemplateBinding Content}" />
</Border>
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}"
VerticalAlignment="Top" HorizontalAlignment="Left"
Margin="36,8,0,0"/>
<Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}"
VerticalAlignment="Top" HorizontalAlignment="Left"
Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(shell:WindowChrome.WindowChrome).ResizeBorderThickness}"
Width="{Binding Source={x:Static shell:SystemParameters2.Current}, Path=SmallIconSize.Width}"
shell:WindowChrome.IsHitTestVisibleInChrome="True"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>

请注意,有一个 Grid 作为根元素,其中包含一些用于自定义窗口 NC 的元素。

更新:

您可能会注意到 MSDN 页面的备注中,它包含以下部分:

WindowStyle.None

WindowChrome

这是自定义 WPF 应用程序窗口外观的两种方法。

但是,设置 Window.WindowStyle属性(property)WindowStyle.None :

This removes the non-client frame from the window and leaves only the client area, to which you can apply a custom style. However, when the non-client frame is removed, you also lose the system features and behaviors that it provides, such as caption buttons and window resizing. Another side effect is that the window will cover the Windows taskbar when it is maximized.

然后 WindowChrome引入以使用 WPF 启用 NC 自定义:

To customize a window while retaining its standard functionality, you can use the WindowChrome class. The WindowChrome class separates the functionality of the window frame from the visuals, and lets you control the boundary between the client and non-client areas of your application window. The WindowChrome class lets you put WPF content in the window frame by extending the client area to cover the non-client area. At the same time, it retains system behaviors through two invisible areas; the resize border and caption areas.

所以回到你的问题,你找到的模板,应该是从MSDN示例代码中复制过来的,但是漏掉了真正的根Grid .Border 上的 Margin 是为了给 NC 一些空间。在 MSDN 示例代码中,ContenPreseter只包含Client区,而NC包含Border , 一个 TextBlock用于窗口标题,以及 Image用于窗口图标。

回顾一下,设置WindowChrome使您能够在 Window.Template 中自定义窗口的 NC 区域.

注意:示例 MSDN 示例代码在 .Net 4.5 中似乎有点过时,System.Windows.Shell.WindowChrome现在在PresentationFramework.dll ,因此代码可能如下所示:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Style="{DynamicResource WindowStyle1}" Icon="Icon1.ico">
<Window.Resources>
<Style x:Key="WindowStyle1" TargetType="{x:Type Window}">
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome />
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Border Background="Red"
Margin="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}}">
<ContentPresenter Content="{TemplateBinding Content}" />
</Border>
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}"
VerticalAlignment="Top" HorizontalAlignment="Left"
Margin="36,8,0,0"/>
<Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}"
VerticalAlignment="Top" HorizontalAlignment="Left"
Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=WindowChrome.WindowChrome.ResizeBorderThickness}"
Width="{Binding Source={x:Static SystemParameters.SmallIconWidth}}"
WindowChrome.IsHitTestVisibleInChrome="True"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button />
</Grid>

关于c# - 与 WindowChrome 一起使用时,窗口模板中的边框边距没有任何效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16609696/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com