2011-08-08 87 views
2

我已经发现了很多关于在组合框或覆盖系统画笔的下拉列表中选择项目的突出显示的问题。这不是我所追求的......我想在选择后摆脱组合框文本框中显示的选定项目的文本高亮显示。但我不想摆脱下拉菜单中的文字突出显示!因此,覆盖系统画笔并不是我所需要的,因为这也会影响下拉菜单中的项目。以下是XAML的完整测试项目。编译+运行并选择一个项目以查看效果。组合框文本框中任何选定项目的文本都会用淡灰色的笔刷突出显示。这就是我想摆脱的..但如何......? 删除组合框选定的项目文本突出显示

<Window.Resources> 
    <Style x:Key="ComboboxDropdownButton" TargetType="{x:Type ToggleButton}"> 
     <Setter Property="MinWidth" Value="0"/> 
     <Setter Property="MinHeight" Value="0"/> 
     <Setter Property="Width" Value="NaN"/> 
     <Setter Property="Height" Value="NaN"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="Black"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <DockPanel SnapsToDevicePixels="True" 
                Background="{TemplateBinding Background}" 
                LastChildFill="False"> 
         <Border x:Name="Border" 
                Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" 
                DockPanel.Dock="Right" 
                Background="WhiteSmoke" 
            CornerRadius="0,3,3,0" 
            BorderThickness="{TemplateBinding BorderThickness}" 
            BorderBrush="{TemplateBinding BorderBrush}" 
                 > 
          <Path Fill="{TemplateBinding Foreground}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M0,0L4.5,4 9,0z"/> 
         </Border> 
        </DockPanel> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter TargetName="Border" Property="Background" Value="White" /> 
         </Trigger> 
         <Trigger Property="IsChecked" Value="True"> 
          <Setter TargetName="Border" Property="Background" Value="White" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="IsEnabled" Value="False"> 
       <Setter Property="Opacity" Value="0.5"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

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

    <Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}"> 
     <Setter Property="SnapsToDevicePixels" Value="true" /> 
     <Setter Property="OverridesDefaultStyle" Value="true" /> 
     <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" /> 
     <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> 
     <Setter Property="ScrollViewer.CanContentScroll" Value="true" /> 
     <Setter Property="MinWidth" Value="120" /> 
     <Setter Property="MinHeight" Value="20" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ComboBox}"> 
        <Grid> 
         <VisualStateManager.VisualStateGroups> 
          <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> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Uneditable" /> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="FocusStates"> 
           <VisualState x:Name="Focused"> 
            <Storyboard> 
             <DoubleAnimation Duration="00:00:00" Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)" To="1"/> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Unfocused"/> 
           <VisualState x:Name="FocusedDropDown"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Duration="00:00:00" Storyboard.TargetName="DropDownBorder" Storyboard.TargetProperty="(UIElement.Visibility)"> 
              <DiscreteObjectKeyFrame KeyTime="00:00:00"> 
               <DiscreteObjectKeyFrame.Value> 
                <Visibility>Visible</Visibility> 
               </DiscreteObjectKeyFrame.Value> 
              </DiscreteObjectKeyFrame> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <ToggleButton x:Name="ToggleButton" 
        Margin="-1" 
        Grid.Column="2" 
        Focusable="False" 
        ClickMode="Press" 
        Style="{StaticResource ComboboxDropdownButton}" 
        IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"> 
         </ToggleButton> 
         <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}" /> 
         <Rectangle x:Name="FocusVisualElement" RadiusX="2" RadiusY="2" Margin="-3" Stroke="Red" StrokeThickness="1" Opacity="0" IsHitTestVisible="false" /> 
         <Rectangle x:Name="BackgroundVisualElement" RadiusX="2" RadiusY="2" Margin="-1" Fill="BlanchedAlmond" Panel.ZIndex="-1" Stroke="Black" StrokeThickness="1" IsHitTestVisible="false" /> 
       <Popup x:Name="PART_Popup" 
        Placement="Bottom" 
        IsOpen="{TemplateBinding IsDropDownOpen}" 
        AllowsTransparency="True" 
        Focusable="False" 
        PopupAnimation="Slide"> 
          <Border x:Name="DropDownBorder" 
           MaxHeight="{TemplateBinding MaxDropDownHeight}" 
           MinWidth="{Binding ActualWidth, ElementName=BackgroundVisualElement}" 
           Background="BlanchedAlmond" 
           BorderBrush="Black" 
           BorderThickness="1" CornerRadius="0,0,3,3"> 
           <ScrollViewer> 
            <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" /> 
           </ScrollViewer> 
          </Border> 
         </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> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

<Grid> 
    <Button Margin="0,0,363,221" /> 
    <ComboBox Width="120" Height="20" Name="comboBox1" SnapsToDevicePixels="True" HorizontalAlignment="Center" VerticalAlignment="Center" SelectedIndex="0" IsEditable="True" IsReadOnly="True"> 
     <ComboBoxItem>item 1</ComboBoxItem> 
     <ComboBoxItem>item 2</ComboBoxItem> 
     <ComboBoxItem>item 3</ComboBoxItem> 
    </ComboBox> 
</Grid> 
</Window> 

编辑:更好的效果,加上前景= “透明” 的PART_EditableTextBox。在下拉列表文本框选定的项目仍然会显示与浅灰刷我想摆脱的突出..

回答

相关问题