Wpf 组合框大小差异

Wpf 组合框大小差异,wpf,xaml,Wpf,Xaml,我在应用程序设置弹出框中有2个组合框。其中一个已定义ItemTemplate,另一个未定义: <StackPanel Orientation="Horizontal" Margin="5"> <Label Content="Accent" VerticalAlignment="Center"/> <ComboBox ItemsSource="{Binding MetroAccents}" SelectedItem=

我在应用程序设置弹出框中有2个组合框。其中一个已定义ItemTemplate,另一个未定义:

<StackPanel Orientation="Horizontal"
        Margin="5">
<Label Content="Accent"
       VerticalAlignment="Center"/>
<ComboBox ItemsSource="{Binding MetroAccents}" 
          SelectedItem="{Binding SelectedAccent}"
          Margin="5">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding Name}"
                   Margin="0"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>



但是,带有ItemTemplate的组合框比其他组合框的高度更高。使用“启用选择”调试工具分析控件时,带有ItemTemplate的组合框似乎有一个带有标签(在模板中定义)的文本块。而另一个组合框只有一个标签。有人知道这是为什么吗?

标签将占用更多空间,因为它的默认样式如下所示:

<Style x:Key="LabelStyle1" TargetType="{x:Type Label}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>  

你可以看到上面有填充物。而
文本块
没有。这就是它占用更多空间的原因。

请记住,
标签
支持带有
\uuu
下划线和其他UI元素作为内容的键绑定。我个人不使用它,除非我需要键绑定,但即使使用
AccessText

标签
也会占用更多空间,因为它的默认样式如下所示:

<Style x:Key="LabelStyle1" TargetType="{x:Type Label}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Padding" Value="5"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>  

你可以看到上面有填充物。而
文本块
没有。这就是它占用更多空间的原因。

请记住,
标签
支持带有
\uuu
下划线和其他UI元素作为内容的键绑定。我个人不使用它,除非我需要键绑定,但即使这样我也使用
AccessText

你真的需要在模板中使用
标签
?关于
TextBlock
?我总是尽量避免
StackPanel
,因为他们无法正确管理空间,尝试使用
Grids
@XAMlMAX将标签替换为文本块修复了问题,谢谢你真的需要模板中的
Label
?关于
TextBlock
?我总是尽量避免
StackPanel
,因为他们没有正确地管理空间,尝试使用
网格构建它
@XAMlMAX将标签替换为文本块修复了这个问题,谢谢。另外,在标签上设置Padding=0也可以完成这项工作,在标签上设置Padding=0也可以完成此工作