wpf组合框填充

kknvjkwl  于 2023-08-07  发布在  其他
关注(0)|答案(3)|浏览(101)

这是一个简单的问题
如何删除组合框的内容和边框之间的空格。例如,如果组合框的选择是“选择1”,则“S”被绘制在组合框控件的最左上方,并且在它和控件的左上方部分之间没有空白间隔。
是我干的

<ComboBox Padding="0"/>

字符串
甚至这个:

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


上面指定的ComboBox位于ListView GridViewColumn中。也许那是在扰乱什么
这不会删除填充。有什么想法吗?

zazmityj

zazmityj1#

你做不到。至少,用默认模板不行。你得自己写。默认模板包括:

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

字符串
这是一个硬编码的空白。对于默认模板,你能做的最好的事情就是使用负填充来偏移硬编码的边距:

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

e5njpo68

e5njpo682#

在Expression Blend中,这是微不足道的:
1.右键单击ComboBox并选择Edit Control Parts (Template)-Edit A Copy
1.从模板的开头删除Margin="2"

68bkxrlz

68bkxrlz3#

接受的答案对我毫无用处。我不得不重写模板中的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>

相关问题