.net 自定义下拉框上的文本溢出

.net 自定义下拉框上的文本溢出,.net,wpf,.net,Wpf,我在尝试在WPF中设置组合框样式时遇到问题。它可以正确渲染,但如果文本大于包含的文本框,则所选选项的文本会溢出到togglebutton上。有办法解决这个问题吗 <Style x:Key="cmbToggle" TargetType="{x:Type ToggleButton}"> <Setter Property="Template"> <Setter.Value> <Contro

我在尝试在WPF中设置组合框样式时遇到问题。它可以正确渲染,但如果文本大于包含的文本框,则所选选项的文本会溢出到togglebutton上。有办法解决这个问题吗

<Style x:Key="cmbToggle" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">

                    <Border Name="cmbBorder" CornerRadius="3" BorderBrush="Silver" BorderThickness="1">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                <GradientStop Color="White" Offset="0" />
                                <GradientStop Color="#FFE9E9E9" Offset="1" />
                            </LinearGradientBrush>
                        </Border.Background>

                        <Border BorderBrush="#FFADADAD" BorderThickness="0,0,0,0" Width="25" Height="25" HorizontalAlignment="Right">
                            <!--The polygon is the triangle for dropdown-->
                            <Image Source="/Images/dropdown-arrow.png" VerticalAlignment="Center" HorizontalAlignment="Left" />                                
                        </Border>

                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="CornerRadius" TargetName="cmbBorder" Value="4,4,0,0"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate x:Key="ComboBoxTextBox"
             TargetType="{x:Type TextBox}">
        <Border x:Name="PART_ContentHost"
      Focusable="False"
      Background="{TemplateBinding Background}" />
    </ControlTemplate>

<!--The combobox consist of two parts, a toggle button and a popup control-->
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="Foreground" Value="black"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Padding" Value="4,3"/>
            <Setter Property="MinHeight" Value="25" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>

                            <!--Binding explanation follows-->
                            <!--The SystemParameters.ComboBoxPopupAnimationKey is the standard roll-down animation for dropdowns-->
                            <Popup Margin="1" x:Name="PART_Popup" AllowsTransparency="true"
                            IsOpen="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
                            Placement="Bottom"
                            PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
                            Grid.ColumnSpan="2"
                            MinWidth="{TemplateBinding ActualWidth}">

                                <Border Name="DropDownBorder" BorderThickness="1,0,1,1" CornerRadius="0,0,4,4" BorderBrush="#FFC4DEFF">
                                    <Border.Background>
                                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                            <GradientStop Color="#FFDEEDFF" Offset="0" />
                                            <GradientStop Color="#C1FFFFFF" Offset="1" />
                                        </LinearGradientBrush>
                                    </Border.Background>
                                    <ScrollViewer CanContentScroll="true">
                                        <ItemsPresenter  />
                                    </ScrollViewer>
                                </Border>

                            </Popup>

                            <TextBox x:Name="PART_EditableTextBox"
                               Style="{x:Null}"
                               Template="{StaticResource ComboBoxTextBox}"
                               HorizontalAlignment="Left"
                               VerticalAlignment="Bottom"
                               Margin="3,3,23,3"
                               Focusable="True"
                               Background="Transparent"
                               Visibility="Hidden"
                               IsReadOnly="{TemplateBinding IsReadOnly}" />

                            <!--
                    Here's some explanation needed:
                    To tell the tooglebutton what to do if it gets checked, we need to bind his property to a source
                    at least, 3 parameters are needed:

                    PATH:   The path to the property we want to bind (isDropDownOpen)
                    MODE:   We want the popup to open AND close EVERYTIME, so we use TwoWay
                    SOURCE: Where to apply the style to? To its parent, the combobox-template!
                    -->
                            <ToggleButton Style="{StaticResource cmbToggle}" Grid.ColumnSpan="2"
                                    IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/>

                            <ContentPresenter HorizontalAlignment="Left"
                                        Margin="5,0,0,0"
                                        VerticalAlignment="Center"
                                        IsHitTestVisible="false"
                                        Content="{TemplateBinding SelectionBoxItem}"
                                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                        />

                        </Grid>

                    </ControlTemplate>
                </Setter.Value>

            </Setter>

        </Style>

文本框的控件模板必须具有名为“PART\u ContentHost”的滚动查看器才能正常工作。当前您有一个
边框
作为替换,这就是渲染错误的原因。将其更改回
ScrollViewer
,问题应该得到解决


还有一个小问题,按钮模板中的箭头宽度为25,但文本框右侧的边距仅为23。将边距更改为
3,3,28,3
,使文本框右侧和按钮之间有三个像素。

谢谢,文本不再溢出,但仍在切换按钮上写入。有什么建议吗?您是否已将文本框上的边距更改为
3,3,28,3
?PART_EditableTextBox?是的,我按照建议做了更改。那么,给ContentPresenter一个
5,0,28,0
的边距。这样做了。非常感谢你!