2011-11-03 24 views
5

Ok, estoy completamente perplejo con esto. Tengo un ComboBox en mi proyecto WPF que tiene IsEditable establecido en verdadero. El usuario puede seleccionar una de las opciones del menú desplegable o escribir algo a su gusto.Texto del cuadro combinado al escribir

Si el usuario escribe o selecciona una opción del menú desplegable, ¿cómo realizo el ajuste del texto?

¿Alguna sugerencia?

Muchas gracias

Respuesta

4

el cuadro combinado utiliza un cuadro de texto para mostrar el contenido .. lo puede invalidar la plantilla y sólo tiene que añadir el "TextWrapping = Wrap" al cuadro de texto "PART_EditableTextbox":

<ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}"> 
    <Grid SnapsToDevicePixels="true"> 
     <Border x:Name="Bd" 
       Background="{TemplateBinding Background}" 
       BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="{TemplateBinding BorderThickness}" 
       Padding="1"> 
      <Grid Grid.IsSharedSizeScope="true"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="1" /> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="Auto" SharedSizeGroup="ComboBoxButton" /> 
       </Grid.ColumnDefinitions> 
       <TextBox x:Name="PART_EditableTextBox" 
          Grid.Column="1" 
          Margin="{TemplateBinding Padding}" 
          HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
          VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
          IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" 
          TextWrapping="Wrap" 
          Style="{StaticResource ComboBoxEditableTextBox}" /> 
       <ToggleButton Grid.ColumnSpan="3" 
           Background="{x:Null}" 
           IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" 
           Style="{StaticResource ComboBoxTransparentButtonStyle}" /> 
      </Grid> 
     </Border> 
     <Popup x:Name="PART_Popup" 
       AllowsTransparency="true" 
       Focusable="false" 
       IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" 
       Placement="Bottom" 
       PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"> 
      <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" 
                   MinWidth="{TemplateBinding ActualWidth}" 
                   MaxHeight="{TemplateBinding MaxDropDownHeight}" 
                   Color="Transparent"> 
       <Border x:Name="DropDownBorder" 
         Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" 
         BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" 
         BorderThickness="1"> 
        <ScrollViewer x:Name="DropDownScrollViewer"> 
         <Grid RenderOptions.ClearTypeHint="Enabled"> 
          <Canvas Width="0" 
            Height="0" 
            HorizontalAlignment="Left" 
            VerticalAlignment="Top"> 
           <Rectangle x:Name="OpaqueRect" 
              Width="{Binding ActualWidth, ElementName=DropDownBorder}" 
              Height="{Binding ActualHeight, ElementName=DropDownBorder}" 
              Fill="{Binding Background, ElementName=DropDownBorder}" /> 
          </Canvas> 
          <ItemsPresenter x:Name="ItemsPresenter" 
              KeyboardNavigation.DirectionalNavigation="Contained" 
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
         </Grid> 
        </ScrollViewer> 
       </Border> 
      </Microsoft_Windows_Themes:SystemDropShadowChrome> 
     </Popup> 
    </Grid> 
    <ControlTemplate.Triggers> 
     <Trigger Property="HasItems" Value="false"> 
      <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95" /> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="false"> 
      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 
      <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" /> 
     </Trigger> 
     <Trigger Property="IsGrouping" Value="true"> 
      <Setter Property="ScrollViewer.CanContentScroll" Value="false" /> 
     </Trigger> 
     <Trigger SourceName="PART_Popup" Property="HasDropShadow" Value="true"> 
      <Setter TargetName="Shdw" Property="Margin" Value="0,0,5,5" /> 
      <Setter TargetName="Shdw" Property="Color" Value="#71000000" /> 
     </Trigger> 
     <Trigger SourceName="DropDownScrollViewer" Property="ScrollViewer.CanContentScroll" Value="false"> 
      <Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}" /> 
      <Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}" /> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 
+1

Ahh, se También puede ser necesario configurar HorizontalContentAlign = Estirar y darle al control un ancho finito. –

Cuestiones relacionadas