删除组合框选定的项目文本突出显示

时间:2011-08-08 10:10:10

标签: wpf xaml combobox

我已经发现很多关于在组合框下拉或覆盖系统画笔中操作要选择的项目突出显示的问题。这不是我想要的。我想在选择之后摆脱组合框文本字段中显示的所选项目的文本突出显示。但我不想摆脱下拉列表中突出显示的文字!因此,覆盖系统画笔不是我需要的,因为这也会影响下拉列表中的项目。以下是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>

编辑以获得更好的效果,将Foreground =“Transparent”添加到PART_EditableTextBox。组合框文本字段中的所选项目仍将以我想摆脱的浅灰色画笔突出显示..

1 个答案:

答案 0 :(得分:4)

如果您使用的是WPF 4,则可以将SelectionBrush的{​​{1}}设置为TextBox

Transparent

您可以在此处详细了解<TextBox x:Name="PART_EditableTextBox" SelectionBrush="Transparent" .../>
http://blogs.msdn.com/b/text/archive/2009/08/28/selection-brush.aspx