我在WPF中有一个无边框且透明的窗口,底部装饰了一些花哨的元素。其中有一个自定义页脚,用一些非传统的曲线和其他东西展示了公司标志。这个窗口需要具备类似于传统窗口那样的可调整大小的右下角控制柄。
不管怎样,我在页脚上放置了自己的ResizeGrip,但是默认的控制柄仍然会出现,并且由于窗口是透明的,它漂浮在空间中。
如何隐藏默认的ResizeGrip?
我在WPF中有一个无边框且透明的窗口,底部装饰了一些花哨的元素。其中有一个自定义页脚,用一些非传统的曲线和其他东西展示了公司标志。这个窗口需要具备类似于传统窗口那样的可调整大小的右下角控制柄。
不管怎样,我在页脚上放置了自己的ResizeGrip,但是默认的控制柄仍然会出现,并且由于窗口是透明的,它漂浮在空间中。
如何隐藏默认的ResizeGrip?
窗口的调整大小手柄的外观由窗口上的ResizeMode依赖属性控制。
如果将其设置为CanResizeWithGrip:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="50" Width="150"
WindowStyle="None" AllowsTransparency="True" Background="#19FFFFFF"
ResizeMode="CanResizeWithGrip">
<Grid></Grid>
</Window>
窗口将会是这样的:
如果设置为 CanResize(默认值):
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="50" Width="150"
WindowStyle="None" AllowsTransparency="True" Background="#19FFFFFF"
ResizeMode="CanResize">
<Grid></Grid>
</Window>
窗口将如下所示:
因此,为了隐藏默认的调整大小握柄,我覆盖了默认的ResizeGrip样式,使其可见性隐藏。借助Expression Blend 2的帮助很容易实现。
<Style TargetType="{x:Type ResizeGrip}">
<Setter Property="MinWidth" Value="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/>
<Setter Property="MinHeight" Value="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarHeightKey}}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ResizeGrip}">
<Grid SnapsToDevicePixels="True" Background="{TemplateBinding Background}">
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Visibility" Value="Hidden"/>
</Style>
然后我在我的自定义窗口装饰上设置了自己的ResizeGrip,样式与默认的grip样式完全相同。
<SolidColorBrush x:Key="ResizeGripperForeground" Color="#B8B4A2"/>
<Style x:Key="VisibleResizeGrip" TargetType="{x:Type ResizeGrip}">
<Setter Property="MinWidth" Value="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"/>
<Setter Property="MinHeight" Value="{DynamicResource {x:Static SystemParameters.HorizontalScrollBarHeightKey}}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ResizeGrip}">
<Grid SnapsToDevicePixels="True" Background="{TemplateBinding Background}">
<Path Fill="White" HorizontalAlignment="Right" Margin="0,0,2,2" VerticalAlignment="Bottom" Data="M 8,0 L 10,0 L 10,2 L 8,2 Z M 4,4 L 6,4 L 6,6 L 4,6 Z M 8,4 L 10,4 L 10,6 L 8,6 Z M 0,8 L 2,8 L 2,10 L 0,10 Z M 4,8 L 6,8 L 6,10 L 4,10 Z M 8,8 L 10,8 L 10,10 L 8,10 Z"/>
<Path Fill="{StaticResource ResizeGripperForeground}" HorizontalAlignment="Right" Margin="0,0,3,3" VerticalAlignment="Bottom" Data="M 8,0 L 10,0 L 10,2 L 8,2 Z M 4,4 L 6,4 L 6,6 L 4,6 Z M 8,4 L 10,4 L 10,6 L 8,6 Z M 0,8 L 2,8 L 2,10 L 0,10 Z M 4,8 L 6,8 L 6,10 L 4,10 Z M 8,8 L 10,8 L 10,10 L 8,10 Z"/>
<Path Data="M8,0L10,0 10,2 8,2z M4,4L6,4 6,6 4,6z M8,4L10,4 10,6 8,6z M0,8L2,8 2,10 0,10z M4,8L6,8 6,10 4,10z M8,8L10,8 10,10 8,10z" Fill="White" HorizontalAlignment="Right" Margin="0,0,2,2" VerticalAlignment="Bottom" />
<Path Data="M8,0L10,0 10,2 8,2z M4,4L6,4 6,6 4,6z M8,4L10,4 10,6 8,6z M0,8L2,8 2,10 0,10z M4,8L6,8 6,10 4,10z M8,8L10,8 10,10 8,10z" Fill="{StaticResource ResizeGripperForeground}" HorizontalAlignment="Right" Margin="0,0,3,3" VerticalAlignment="Bottom" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>