【问题标题】:RenderSize of Grid control have difference in Windows 10 [.NET Framework 4.6.2] and Windows 7[.NET Framework 4]Grid 控件的 RenderSize 在 Windows 10 [.NET Framework 4.6.2] 和 Windows 7[.NET Framework 4] 中有所不同
【发布时间】: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中有所不同。 从MSDNRenderSize 是元素的最终渲染大小。

我的疑问是为什么这个RenderSize 在 Windows 10 和 Windows 7 中有不同的值?

【问题讨论】:

    标签: wpf windows-7 grid windows-10 appearance


    【解决方案1】:

    Windows 10 和 Windows 7 对 WPF 控件使用不同的默认主题。除非您覆盖默认模板和/或更改必要的属性,否则按钮和组合框之类的东西看起来会略有不同。您还可以使用 Expression blend 来“转储”默认模板以查看详细信息。

    如果您在 StackOverflow 上进行一些搜索,还有许多其他关于主题的问题。 IE: where-to-find-wpf-classic-theme-as-xaml

    【讨论】:

    • 我在.NET Framework 4(使用Blend 2012)和4.6.2(使用Blend 2017)中检查了组合框的默认控件模板。据观察,某些属性值(如 BorderThickness 和 Padding 等)存在一些差异。知道为什么会有这种差异吗?更改 .NET Frame 工作时是否更新了控件模板?
    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 2017-04-06
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    相关资源
    最近更新 更多