如何在WPF中为ComboItem添加边距

时间:2016-09-08 07:05:38

标签: c# wpf combobox

我想更改全局ComboItems列表的边距,而不是每个,以在ComboBox和项目列表之间创建一个空格。

我解释一下。我知道我可以使用

为ComboItem添加样式
 <Style TargetType="ComboBoxItem">

但在我的情况下,我想更改项目列表的边距,而不是每个项目的边距。

我不知道如何到达酒店以修改此保证金。

如果你有任何想法,谢谢你

1 个答案:

答案 0 :(得分:1)

您可以提取组合框like that的默认控件模板,或者像这样:

enter image description here

然后你调整了这一部分(我在Margin上添加了ItemsPresenter):

<Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
    <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
        <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
            <ScrollViewer x:Name="DropDownScrollViewer">
                <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
                    <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                        <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
                    </Canvas>
                    <ItemsPresenter Margin="0,10,0,10" x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Grid>
            </ScrollViewer>
        </Border>
    </Themes:SystemDropShadowChrome>
</Popup>

你明白了:

enter image description here