2011-11-25 83 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

這也可以,謝謝!但我現在已經完全刪除了模板。 –