在特定阈值下调整 wpf 控件大小时出现不希望的剪辑
Posted
技术标签:
【中文标题】在特定阈值下调整 wpf 控件大小时出现不希望的剪辑【英文标题】:Undesired clipping when resizing wpf control under certain tresshold 【发布时间】:2011-12-24 16:45:48 【问题描述】:在 WPF 中开发自定义控件时,我注意到一种奇怪的剪切行为。当我在某个最小边界/tress-hold 下调整控件大小时,出于我不明白的原因,它决定裁剪控件的顶部 24 个像素。
如果有人能解释我如何在没有剪裁行为的情况下实现所需的图形效果,将不胜感激。
该行为发生在以下 XAML 中。请注意,XAML 被精简以使其符合标准的 .net 4.0 控件。
<Style x:Key="ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly=x:Type DataGrid" TargetType="x:Type Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type Button">
<Grid>
<Rectangle x:Name="Border" Fill="DynamicResource x:Static SystemColors.ControlBrushKey" SnapsToDevicePixels="True"/>
<Polygon x:Name="Arrow" Fill="Black" HorizontalAlignment="Right" Margin="8,8,3,3" Opacity="0.15" Points="0,10 10,10 10,0" Stretch="Uniform" VerticalAlignment="Bottom"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Stroke" TargetName="Border" Value="DynamicResource x:Static SystemColors.ControlDarkBrushKey"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Fill" TargetName="Border" Value="DynamicResource x:Static SystemColors.ControlDarkBrushKey"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" TargetName="Arrow" Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="DataGridStyle1" TargetType="x:Type DataGrid">
<Setter Property="Background" Value="DynamicResource x:Static SystemColors.ControlBrushKey"/>
<Setter Property="Foreground" Value="DynamicResource x:Static SystemColors.ControlTextBrushKey"/>
<Setter Property="BorderBrush" Value="#FF688CAF"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type DataGrid">
<Border Background="Transparent" Margin="0,24,0,0" >
<Border BorderBrush="TemplateBinding BorderBrush"
BorderThickness="TemplateBinding BorderThickness"
Background="TemplateBinding Background" Padding="TemplateBinding Padding"
SnapsToDevicePixels="True">
<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
<ScrollViewer.Template>
<ControlTemplate TargetType="x:Type ScrollViewer">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="x:Static DataGrid.SelectAllCommand" Focusable="false" Style="DynamicResource ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly=x:Type DataGrid" Visibility="Binding HeadersVisibility, ConverterParameter=x:Static DataGridHeadersVisibility.All, Converter=x:Static DataGrid.HeadersVisibilityConverter, RelativeSource=RelativeSource AncestorType=x:Type DataGrid" Width="Binding CellsPanelHorizontalOffset, RelativeSource=RelativeSource AncestorType=x:Type DataGrid"/>
<DataGridColumnHeadersPresenter Grid.Row="0" x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="Binding HeadersVisibility, ConverterParameter=x:Static DataGridHeadersVisibility.Column, Converter=x:Static DataGrid.HeadersVisibilityConverter, RelativeSource=RelativeSource AncestorType=x:Type DataGrid"/>
<Grid VerticalAlignment="Top" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="1" ClipToBounds="False">
<Grid.RenderTransform>
<TranslateTransform Y="-24" />
</Grid.RenderTransform>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource=RelativeSource AncestorType=x:Type DataGrid"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Rectangle Fill="Yellow" Grid.Row="1" Grid.ColumnSpan="3" Grid.Column="1" Height="48" />
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels"/>
</ScrollViewer>
</Border>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsGrouping" Value="true">
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</Trigger>
</Style.Triggers>
</Style>
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="12,12,0,12" Name="dataGrid1" Width="868" FrozenColumnCount="2"
ScrollViewer.HorizontalScrollBarVisibility="Visible" CanUserSortColumns="False" CanUserReorderColumns="False"
AreRowDetailsFrozen="True" CanUserAddRows="False" Style="StaticResource DataGridStyle1"
ScrollViewer.VerticalScrollBarVisibility="Visible">
<DataGrid.Columns>
<DataGridTextColumn Header="Layer Name" Width="250" IsReadOnly="True" />
<DataGridHyperlinkColumn Header="" Width="150" IsReadOnly="True" />
<DataGridTemplateColumn Width="*" HeaderStyle="StaticResource DataGridColumnHeaderStyle" IsReadOnly="True">
<DataGridTemplateColumn.Header>
<Grid Height="18">
<TextBlock Text="100.0f" Margin="0,-10" />
</Grid>
</DataGridTemplateColumn.Header>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="x:Type GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type GroupItem">
<StackPanel>
<TextBlock Text="test" />
<ItemsPresenter />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
【问题讨论】:
【参考方案1】:所以经过几天的摆弄,我发现罪魁祸首是 GetLayoutClip 方法。如果控件的边界小于最小边界,此方法仅返回裁剪几何。
覆盖此行为的最简单解决方案是使自定义控件继承自上述基本控件。并在考虑额外边距的情况下构造新的剪裁几何。
【讨论】:
以上是关于在特定阈值下调整 wpf 控件大小时出现不希望的剪辑的主要内容,如果未能解决你的问题,请参考以下文章
C#,实现窗口大小改变时窗口内的控件自动调整位置和大小时,groupbox内的控件为啥不自动调整