【发布时间】:2018-12-13 11:52:46
【问题描述】:
我有一个自定义组合框样式如下
<Style x:Key="SmallArrowComboBoxStyle" TargetType="{x:Type ComboBox}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="#FF1E1E1E"/>
<Setter Property="BorderBrush" Value="#454545"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="FontFamily" Value="Segoe UI,Meiryo UI" />
<Setter Property="Background" Value="#1C1C1C" />
<Setter Property="Foreground" Value="#FFFFFF" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Grid x:Name="MainGrid1" SnapsToDevicePixels="true" Margin="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
</Grid.ColumnDefinitions>
<Popup x:Name="PART_Popup1" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1,0,1,1" Placement="Bottom" >
<Border x:Name="Shdw1" BorderBrush ="#FF4289C4" Background="#FF4289C4" BorderThickness="1" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid1}">
<Border x:Name="DropDownBorder" BorderThickness="0" Background="#1E1E1E">
<Grid>
<ScrollViewer x:Name="DropDownScrollViewer1" Template="{DynamicResource ScrollViewerControlTemplate1}">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
<Rectangle x:Name="OpaqueRect1" Fill="{Binding Background, ElementName=DropDownBorder}"
Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
</Canvas>
<ItemsPresenter x:Name="ItemsPresenter1" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Grid>
</ScrollViewer>
<Slider x:Name="CustomSliderVertical" Margin="0,0,-12,0" Maximum="100" SmallChange="1" LargeChange="10" TickPlacement="BottomRight" TickFrequency="10"
Style="{StaticResource CustomSliderStyle}" Foreground="{DynamicResource LimeBrush}" HorizontalAlignment="Right" Delay="1"
Orientation="Vertical" Focusable="False" Grid.Column="0" Grid.Row="1" Panel.ZIndex="1"/>
</Grid>
</Border>
</Border>
</Popup>
<ToggleButton BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}" Grid.ColumnSpan="2"
IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource SmallArrowComboBoxToggleButton}" BorderThickness="{TemplateBinding BorderThickness}"/>
<ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Name="ContentSite"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}"
ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
IsHitTestVisible="false" Margin="{TemplateBinding Padding}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
<TextBox x:Name="PART_EditableTextBoxSmall" Width="150" Height="40"
Style="{x:Null}"
Template="{StaticResource ComboBoxTextBoxSmall}"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Margin="3,3,23,3"
Focusable="True"
Background="Transparent"
Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsEditable" Value="true">
<Setter Property="IsTabStop" Value="false"/>
<Setter TargetName="PART_EditableTextBoxSmall" Property="Visibility" Value="Visible"/>
<Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
<Setter Property="Foreground" TargetName="PART_EditableTextBoxSmall" Value="#FFFFFF"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我准备了一个带有组合框的测试应用程序,并将这种样式应用于组合框。 使用
准备测试应用程序.NET Framework 4 和 4.6.2
使用两者测试组合框的外观
Windows-10(.NET Framework 4.6.2) 和 Windows-7(.NET Framework 4)
但 Windows 10 和 Windows 7 之间存在外观差异。
那是组合框文本的对齐方式不同。
即组合框的填充在 Windows 10 和 Windows 7 中是不同的。但是这些填充值没有在代码中明确设置。
- 在 Windows 7 中,填充值为“4,3,4,3”
- 在 Windows 10 中,填充值为“6,3,5,3”
检查时发现“MainGrid1”(上述样式代码中的网格布局控件)的RenderSize在Windows 10和Windows 7中有所不同。
从MSDN,RenderSize 是元素的最终渲染大小。
我的疑问是为什么这个RenderSize 在 Windows 10 和 Windows 7 中有不同的值?
【问题讨论】:
标签: wpf windows-7 grid windows-10 appearance