2011-11-25 85 views
0

在我的WPF应用程序中,我有一个150px固定宽度的可编辑组合框。尽管如果项目的长度大于组合框的长度,选择颜色会从组合框区域溢出,并与切换按钮以及右侧的控件重叠。可编辑的组合框选择文本溢出

截图:

使用的控件模板如下:

<ControlTemplate TargetType="{x:Type ComboBox}"> 
    <Grid> 
    <VisualStateManager.VisualStateGroups> 
     <VisualStateGroup x:Name="CommonStates"> 
     <VisualState x:Name="Normal" /> 
     <VisualState x:Name="MouseOver" /> 
     <VisualState x:Name="Disabled"> 
      <Storyboard> 
      <ColorAnimationUsingKeyFrames Storyboard.TargetName="PART_EditableTextBox" 
              Storyboard.TargetProperty="(TextElement.Foreground). 
       (SolidColorBrush.Color)"> 
       <EasingColorKeyFrame KeyTime="0" 
            Value="{StaticResource DisabledForegroundColor}" /> 
      </ColorAnimationUsingKeyFrames> 
      </Storyboard> 
     </VisualState> 
     </VisualStateGroup> 
     <VisualStateGroup x:Name="EditStates"> 
     <VisualState x:Name="Editable"> 
      <Storyboard> 
      <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" 
              Storyboard.TargetName="PART_EditableTextBox"> 
       <DiscreteObjectKeyFrame KeyTime="0" 
             Value="{x:Static Visibility.Visible}" /> 
      </ObjectAnimationUsingKeyFrames> 
      <ObjectAnimationUsingKeyFrames 
       Storyboard.TargetProperty="(UIElement.Visibility)" 
              Storyboard.TargetName="ContentSite"> 
       <DiscreteObjectKeyFrame KeyTime="0" 
             Value="{x:Static Visibility.Hidden}" /> 
      </ObjectAnimationUsingKeyFrames> 
      </Storyboard> 
     </VisualState> 
     <VisualState x:Name="Uneditable" /> 
     </VisualStateGroup> 
    </VisualStateManager.VisualStateGroups> 
    <ToggleButton x:Name="ToggleButton" 
        Template="{StaticResource ComboBoxToggleButton}" 
        Grid.Column="2" 
        Focusable="false" 
        ClickMode="Press" 
        IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, 
     RelativeSource={RelativeSource TemplatedParent}}"/> 
    <ContentPresenter x:Name="ContentSite" 
         IsHitTestVisible="False" 
         Content="{TemplateBinding SelectionBoxItem}" 
         ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
         ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
         Margin="3,3,23,3" 
         VerticalAlignment="Stretch" 
         HorizontalAlignment="Left"> 
    </ContentPresenter> 
    <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}" /> 
    <Popup x:Name="Popup" 
      Placement="Bottom" 
      IsOpen="{TemplateBinding IsDropDownOpen}" 
      AllowsTransparency="True" 
      Focusable="False" 
      PopupAnimation="Slide"> 
     <Grid x:Name="DropDown" 
      SnapsToDevicePixels="True" 
      MinWidth="{TemplateBinding ActualWidth}" 
      MaxHeight="{TemplateBinding MaxDropDownHeight}"> 
     <Border x:Name="DropDownBorder" 
       BorderThickness="1"> 
      <Border.BorderBrush> 
      <SolidColorBrush Color="{DynamicResource BorderMediumColor}" /> 
      </Border.BorderBrush> 
      <Border.Background> 
      <SolidColorBrush Color="{DynamicResource ControlLightColor}" /> 
      </Border.Background> 
     </Border> 
     <ScrollViewer Margin="4,6,4,6" 
         SnapsToDevicePixels="True"> 
      <StackPanel IsItemsHost="True" 
         KeyboardNavigation.DirectionalNavigation="Contained" /> 
     </ScrollViewer> 
     </Grid> 
    </Popup> 
    </Grid> 
    <ControlTemplate.Triggers> 
    <Trigger Property="HasItems" 
       Value="false"> 
     <Setter TargetName="DropDownBorder" 
       Property="MinHeight" 
       Value="95" /> 
    </Trigger> 
    <Trigger Property="IsGrouping" 
       Value="true"> 
     <Setter Property="ScrollViewer.CanContentScroll" 
       Value="false" /> 
    </Trigger> 
    <Trigger SourceName="Popup" 
       Property="AllowsTransparency" 
       Value="true"> 
     <Setter TargetName="DropDownBorder" 
       Property="CornerRadius" 
       Value="4" /> 
     <Setter TargetName="DropDownBorder" 
       Property="Margin" 
       Value="0,2,0,0" /> 
    </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 

任何想法发生什么事,怎么能解决吗?

编辑文本框模板:

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

我无法复制它。我带上了您的ControlTemplate并设置了固定颜色而不是您的StaticResources,并省略了TextBox模板,因为您没有在示例代码中提供它。它对我来说很有效,编辑文本框的选择长度和组合框一样宽。 所以你的问题应该是由TextBox的ComboBoxTextBox模板造成的...... – SvenG

+0

模板可能是原因,是的。好东西,我错过了发布!感谢您的测试。 –

+0

@SvenG:问题已解决,如果将其添加为答案,我会将其标记为已解决!谢谢:) –

回答

0

正如在评论部分上述问题是ComboBoxTextBox模板。

1

我刚才已经回答a similar question ...

的原因是你overwritting对TextBox的东西没有实现自动滚动的默认控件模板,让你的文字仅仅是它被赋予的范围之外继续。

TextBox.ControlTemplate更改为ScrollViewer而不是Border,它将正常工作。

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

这也可以,谢谢!但我现在已经完全删除了模板。 –