wpf 组合框填充

问题描述 投票:0回答:3

这是一个简单的问题

如何删除组合框内容与其边框之间的空格。例如。如果组合框的选择是“选择 1”,则“S”将绘制在 ComboBox 控件的左上角,并且与控件的左上角部分之间没有空白间距。

我做了这个

<ComboBox Padding="0"/>

即使这样:

<ComboBox.ItemContainerStyle>
  <Style TargetType="ComboBoxItem">
    <Setter Property="Padding" Value="0"/>
  </Style>
</ComboBox.ItemContainerStyle>

上面指定的 ComboBox 位于 ListView GridViewColumn 内。也许这会搞乱一些东西。

这不会删除填充。有什么想法吗?

wpf combobox
3个回答
3
投票

你不能。至少,默认模板不是这样。你必须自己写。默认模板包括:

<DockPanel Margin="2">
    <TextBox .../>
</DockPanel>

这将是一个硬编码的边距。使用默认模板所能做的最好的事情就是使用负填充来抵消硬编码的边距:

<ComboBox Padding="-2">
    <ComboBoxItem >Selected</ComboBoxItem>
</ComboBox>

1
投票

在 Expression Blend 中,这是微不足道的:

  1. 右键单击组合框并选择
    Edit Control Parts (Template)
    -
    Edit A Copy
  2. 删除模板开头的
    Margin="2"

0
投票

接受的答案对我没有任何帮助。我必须像这样覆盖模板中的 ContentPresenter Margin 值:

                            <ContentPresenter
                            Name="ContentSite"
                            ...
                            Margin="0"
                            .../>

如果您想自己测试的话,这是完整的样式。

<Window.Resources>
    <Style x:Key="ComboBoxStyle" TargetType="ComboBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton
                            Name="ToggleButton"
                            ClickMode="Press"
                            IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                            Focusable="false"
                            IsHitTestVisible="false"
                            Padding="0">
                            <ToggleButton.Template>
                                <ControlTemplate TargetType="ToggleButton">
                                    <Border
                                        x:Name="Border"
                                        Background="{TemplateBinding Background}"
                                        BorderBrush="{TemplateBinding BorderBrush}"
                                        BorderThickness="{TemplateBinding BorderThickness}">
                                        <TextBlock Text="{TemplateBinding Content}" Padding="2"/>
                                    </Border>
                                </ControlTemplate>
                            </ToggleButton.Template>
                        </ToggleButton>
                        <ContentPresenter
                            Name="ContentSite"
                            IsHitTestVisible="False"
                            Content="{TemplateBinding SelectionBoxItem}"
                            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                            Margin="0"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left"/>
                        <Popup
                            Name="Popup"
                            AllowsTransparency="True"
                            Focusable="False"
                            IsOpen="{TemplateBinding IsDropDownOpen}"
                            PopupAnimation="Slide">
                            <Grid
                                Name="DropDown"
                                SnapsToDevicePixels="True"
                                MinWidth="{TemplateBinding ActualWidth}"
                                MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                <Border
                                    x:Name="DropDownBorder"
                                    Background="{StaticResource {x:Static SystemColors.WindowBrushKey}}"
                                    BorderThickness="1"
                                    BorderBrush="#888888"/>
                                <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                    <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Border" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </Trigger>
                        <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                            <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                            <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0"/>
                        </Trigger>
                        <Trigger Property="IsEditable" Value="true">
                            <Setter Property="IsTabStop" Value="false"/>
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource {x:Static SystemColors.WindowBrushKey}}"/>
                            <Setter TargetName="Border" Property="BorderBrush" Value="#888888"/>
                            <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                            <Setter TargetName="ContentSite" Property="IsHitTestVisible" Value="False"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
© www.soinside.com 2019 - 2024. All rights reserved.