如何改变WPF组合框SelectedText背景颜色? [英] How to change WPF ComboBox SelectedText BackGround Color?

查看:823
本文介绍了如何改变WPF组合框SelectedText背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF的MVVM组合框和我风格的组合框的变化组合框的被弹框和文本框。

当我滚动组合框列表项thier的背景是粉红色是我chnaged。但是,从组合框列表中选择一个项目之后,在组合框中项目选定值具有蓝色背景。这是默认为在Windows窗体和WPF一个combobbox。

查看图片了解更多详情。

输入图像的描述在这里

我怎样才能改变这种状况,在组合框中文本框控件selectedtext背景颜色

组合框有

IsEditable = TRUE 属性集

解决方案

 < ResourceDictionary中的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p $ psentation
                    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml>
    < ResourceDictionary.MergedDictionaries>
        < ResourceDictionary中源=Brushes.xaml/>
    < /ResourceDictionary.MergedDictionaries>

    <的ControlTemplate的TargetType =切换按钮X:键=ComboBoxToggleButtonTemplate>
        <电网>
            < Grid.ColumnDefinitions>
                < ColumnDefinition />
                < ColumnDefinition WIDTH =20/>
            < /Grid.ColumnDefinitions>
            < BORDER BorderBrush ={的StaticResource LightBrush}
                    CornerRadius =0
                    了borderThickness =1
                    名称=边境
                    背景={的StaticResource WhiteBrush}
                    Grid.ColumnSpan =2/>
            < BORDER保证金=1
          BorderBrush ={的StaticResource DarkBrush}
          CornerRadius =0
          了borderThickness =0
          背景={的StaticResource WhiteBrush}
          Grid.Column =0/>
            <路径
          数据=M0,0L4,4 8,0z
          的Horizo​​ntalAlignment =中心
          填写={DynamicResource DefaultBrush}
          NAME =箭
          VerticalAlignment =中心
          宽度=8
          Grid.Column =1/>
        < /网格>
        < ControlTemplate.Triggers>
            <触发属性=ToggleButton.IsCheckedVALUE =真>
                < setter属性=Panel.Background的TargetName =边界值={DynamicResource DefaultBrush}/>
                < setter属性=Shape.Fill的TargetName =箭值={的StaticResource WhiteBrush}/>
                < setter属性=Border.BorderBrush的TargetName =边界值={DynamicResource DefaultBrush}/>
                < setter属性=Border.BorderThickness的TargetName =边界值=1,1,1,0>< /二传手>
            < /触发>
            <触发属性=UIElement.IsEnabledVALUE =FALSE>
                < setter属性=Panel.Background的TargetName =边界值={的StaticResource DisabledBackgroundBrush}/>
                < setter属性=Border.BorderBrush的TargetName =边界值={的StaticResource DisabledBorderBrush}/>
                < setter属性=TextElement.ForegroundVALUE ={的StaticResource DisabledForegroundBrush}/>
                < setter属性=Shape.Fill的TargetName =箭值=#66FFFFFF/>
            < /触发>
        < /ControlTemplate.Triggers>
    < /控件模板>
    <的ControlTemplate的TargetType =文本框X:键=ComboBoxTextBoxTemplate>
        < BORDER
        NAME =PART_ContentHost背景={DynamicResource LightDefaultBrush}
        可调焦=FALSE/>
    < /控件模板>
    <风格的TargetType ={X:类型ComboBoxItem}>
        < setter属性=UIElement.SnapsToDevicePixelsVALUE =真/>
        < setter属性=FrameworkElement.FocusVisualStyleVALUE ={X:空}/>
        < setter属性=TextElement.ForegroundVALUE ={的StaticResource ForeGroundBrush}/>
        < setter属性=FrameworkElement.OverridesDefaultStyleVALUE =真/>
        < setter属性=Control.Template>
            < Setter.Value>
                <的ControlTemplate的TargetType ={X:类型ComboBoxItem}>
                    < BORDER
              名称=边境
              SnapsToDevicePixels =真
              填充=3,2,2,2>
                        <内容presenter
                的ContentTemplate ={TemplateBinding ContentControl.ContentTemplate}
                内容={TemplateBinding ContentControl.Content}/>
                    < /边框>
                    < ControlTemplate.Triggers>
                        <触发属性=ComboBoxItem.IsHighlightedVALUE =真>
                            < setter属性=Panel.Background的TargetName =边界值={DynamicResource DefaultBrush}/>
                            < setter属性=TextElement.ForegroundVALUE =白>< /二传手>
                        < /触发>
                        <触发属性=UIElement.IsEnabledVALUE =FALSE>
                            < setter属性=TextElement.ForegroundVALUE ={的StaticResource DisabledForegroundBrush}/>
                        < /触发>
                    < /ControlTemplate.Triggers>
                < /控件模板>
            < /Setter.Value>
        < /二传手>
    < /样式和GT;
    <风格的TargetType ={X:类型组合框}>
        < setter属性=UIElement.SnapsToDevicePixelsVALUE =真/>
        < setter属性=FrameworkElement.OverridesDefaultStyleVALUE =真/>
        < setter属性=ScrollViewer.Horizo​​ntalScrollBarVisibilityVALUE =自动/>
        < setter属性=ScrollViewer.VerticalScrollBarVisibilityVALUE =自动/>
        < setter属性=ScrollViewer.CanContentScrollVALUE =真/>
        < setter属性=TextElement.ForegroundVALUE ={的StaticResource ForeGroundBrush}/>
        < setter属性=FrameworkElement.FocusVisualStyleVALUE ={X:空}/>
        < setter属性=高度值=25>< /二传手>
        < setter属性=保证金VALUE =0,2,0,2>< /二传手>
        < setter属性=Control.Template>
            < Setter.Value>
                <的ControlTemplate的TargetType =组合框>
                    <电网>
                        <切换按钮
                ClickMode =preSS
                NAME =切换按钮
                =器isChecked{绑定路径= IsDropDownOpen,的RelativeSource = {的RelativeSource TemplatedParent},模式=双向}
                可调焦=FALSE
                Grid.Column =2
                模板={的StaticResource ComboBoxToggleButtonTemplate}/>
                        <内容presenter
                保证金=3,3,23,3
                的Horizo​​ntalAlignment =左
                NAME =ContentSite
                VerticalAlignment =中心
                的ContentTemplate ={TemplateBinding ComboBox.SelectionBoxItemTemplate}
                内容={TemplateBinding ComboBox.SelectionBoxItem}
                IsHitTestVisible =FALSE/>
                        <文本框
                保证金=3,1,1,1
                能见度=隐藏
                的Horizo​​ntalAlignment =左
                NAME =PART_EditableTextBox
                背景=透明
                VerticalAlignment =中心
                风格={X:空}
                的IsReadOnly =FALSE
                可调焦=FALSE
                XML:空间=preserve
                模板={的StaticResource ComboBoxTextBoxTemplate}/>
            <弹出
                放置=底
                NAME =弹出
                可调焦=FALSE
                AllowsTransparency =真
                ISOPEN ={TemplateBinding ComboBox.IsDropDownOpen}
                PopupAnimation =幻灯片>
              <电网
                  了minWidth ={TemplateBinding FrameworkElement.ActualWidth}
                  了maxHeight ={TemplateBinding ComboBox.MaxDropDownHeight}
                  NAME =下拉
                  SnapsToDevicePixels =真>
                < BORDER
                    BorderBrush ={DynamicResource DefaultBrush}
                    了borderThickness =1,1,1,1
                    NAME =DropDownBorder
                    背景={的StaticResource WhiteBrush}/>
                < ScrollViewer的保证金=1,0,1,0
                    SnapsToDevicePixels =真>
                  <项目presenter
                      KeyboardNavigation.DirectionalNavigation =含有/>
                < /的ScrollViewer>
              < /网格>
            < /弹出>
          < /网格>
                    < ControlTemplate.Triggers>
                        <触发属性=ItemsControl.HasItemsVALUE =FALSE>
                            < setter属性=FrameworkElement.MinHeight的TargetName =DropDownBorderVALUE =95/>
                        < /触发>
                        <触发属性=UIElement.IsEnabledVALUE =FALSE>
                            < setter属性=TextElement.ForegroundVALUE ={的StaticResource DisabledForegroundBrush}/>
                        < /触发>
                        <触发属性=ItemsControl.IsGroupingVALUE =真>
                            < setter属性=ScrollViewer.CanContentScrollVALUE =FALSE/>
                        < /触发>
                        <触发属性=Window.AllowsTransparencySOURCENAME =弹出VALUE =真>
                            < setter属性=Border.CornerRadius的TargetName =DropDownBorder值=0/>
                            < setter属性=Border.BorderThickness的TargetName =DropDownBorderVALUE =1,0,1,1/>
                            < setter属性=FrameworkElement.Margin的TargetName =DropDownBorderVALUE =0,0,0,0/>
                        < /触发>
                        <触发属性=ComboBox.IsEditableVALUE =真>
                            < setter属性=KeyboardNavigation.IsTabStopVALUE =FALSE/>
                            < setter属性=UIElement.Visibility的TargetName =PART_EditableTextBoxVALUE =可见/>
                            < setter属性=UIElement.Visibility的TargetName =ContentSiteVALUE =隐藏/>
                        < /触发>
                    < /ControlTemplate.Triggers>
                < /控件模板>
            < /Setter.Value>
        < /二传手>
    < /样式和GT;
< / ResourceDictionary中>
 

I have a Combobox in WPF-MVVM and i have styled the combobox with changes in the popdown box and textbox of combobox.

When i scroll the combobox listitem thier background is pink is what i have chnaged. But after selecting a item from the combobox list, the selected value in combobox item has blue background. which is the default for a combobbox in both Windows Form and WPF.

See the image for more details.

How can i change that selectedtext background color in the combobox textbox control

The combobox has

IsEditable=True property set

解决方案

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Brushes.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <ControlTemplate TargetType="ToggleButton" x:Key="ComboBoxToggleButtonTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition Width="20" />
            </Grid.ColumnDefinitions>
            <Border BorderBrush="{StaticResource LightBrush}" 
                    CornerRadius="0" 
                    BorderThickness="1"
                    Name="Border" 
                    Background="{StaticResource WhiteBrush}" 
                    Grid.ColumnSpan="2" />
            <Border Margin="1"
          BorderBrush="{StaticResource DarkBrush}"
          CornerRadius="0"
          BorderThickness="0"
          Background="{StaticResource WhiteBrush}"
          Grid.Column="0" />
            <Path          
          Data="M0,0L4,4 8,0z"
          HorizontalAlignment="Center"
          Fill="{DynamicResource DefaultBrush}"
          Name="Arrow"
          VerticalAlignment="Center"
          Width="8"
          Grid.Column="1" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="ToggleButton.IsChecked" Value="True">
                <Setter Property="Panel.Background" TargetName="Border" Value="{DynamicResource DefaultBrush}"/>
                <Setter Property="Shape.Fill" TargetName="Arrow" Value="{StaticResource WhiteBrush}"/>
                <Setter Property="Border.BorderBrush" TargetName="Border" Value="{DynamicResource DefaultBrush}"/>
                <Setter Property="Border.BorderThickness" TargetName="Border" Value="1,1,1,0"></Setter>
            </Trigger>
            <Trigger Property="UIElement.IsEnabled" Value="False">
                <Setter Property="Panel.Background" TargetName="Border" Value="{StaticResource DisabledBackgroundBrush}"/>
                <Setter Property="Border.BorderBrush" TargetName="Border" Value="{StaticResource DisabledBorderBrush}"/>
                <Setter Property="TextElement.Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                <Setter Property="Shape.Fill" TargetName="Arrow" Value="#66FFFFFF"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>    
    <ControlTemplate TargetType="TextBox" x:Key="ComboBoxTextBoxTemplate">
        <Border
        Name="PART_ContentHost" Background="{DynamicResource LightDefaultBrush}"
        Focusable="False" />                
    </ControlTemplate>    
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="UIElement.SnapsToDevicePixels" Value="True"/>
        <Setter Property="FrameworkElement.FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="TextElement.Foreground"  Value="{StaticResource ForeGroundBrush}"/>
        <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True"/>
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                    <Border
              Name="Border"
              SnapsToDevicePixels="True"
              Padding="3,2,2,2">
                        <ContentPresenter
                ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
                Content="{TemplateBinding ContentControl.Content}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ComboBoxItem.IsHighlighted" Value="True">
                            <Setter Property="Panel.Background" TargetName="Border" Value="{DynamicResource DefaultBrush}"/>                            
                            <Setter Property="TextElement.Foreground" Value="White"></Setter>
                        </Trigger>
                        <Trigger Property="UIElement.IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type ComboBox}">
        <Setter Property="UIElement.SnapsToDevicePixels" Value="True"/>
        <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True"/>
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
        <Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
        <Setter Property="TextElement.Foreground" Value="{StaticResource ForeGroundBrush}"/>
        <Setter Property="FrameworkElement.FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Height" Value="25"></Setter>
        <Setter Property="Margin" Value="0,2,0,2"></Setter>
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton
                ClickMode="Press"
                Name="ToggleButton"
                IsChecked="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
                Focusable="False"
                Grid.Column="2"
                Template="{StaticResource ComboBoxToggleButtonTemplate}"/>
                        <ContentPresenter
                Margin="3,3,23,3"
                HorizontalAlignment="Left"
                Name="ContentSite"
                VerticalAlignment="Center"
                ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"
                Content="{TemplateBinding ComboBox.SelectionBoxItem}"
                IsHitTestVisible="False" />
                        <TextBox
                Margin="3,1,1,1"
                Visibility="Hidden"
                HorizontalAlignment="Left"
                Name="PART_EditableTextBox"
                Background="Transparent"
                VerticalAlignment="Center"
                Style="{x:Null}"
                IsReadOnly="False"
                Focusable="False"
                xml:space="preserve"                            
                Template="{StaticResource ComboBoxTextBoxTemplate}"/>
            <Popup
                Placement="Bottom"
                Name="Popup"
                Focusable="False"               
                AllowsTransparency="True"               
                IsOpen="{TemplateBinding ComboBox.IsDropDownOpen}"
                PopupAnimation="Slide">
              <Grid
                  MinWidth="{TemplateBinding FrameworkElement.ActualWidth}"
                  MaxHeight="{TemplateBinding ComboBox.MaxDropDownHeight}"
                  Name="DropDown"
                  SnapsToDevicePixels="True">
                <Border
                    BorderBrush="{DynamicResource DefaultBrush}"
                    BorderThickness="1,1,1,1"
                    Name="DropDownBorder"
                    Background="{StaticResource WhiteBrush}"/>
                <ScrollViewer Margin="1,0,1,0"
                    SnapsToDevicePixels="True">
                  <ItemsPresenter
                      KeyboardNavigation.DirectionalNavigation="Contained" />
                </ScrollViewer>
              </Grid>
            </Popup>
          </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="ItemsControl.HasItems" Value="False">
                            <Setter Property="FrameworkElement.MinHeight" TargetName="DropDownBorder" Value="95"/>
                        </Trigger>
                        <Trigger Property="UIElement.IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                        <Trigger Property="ItemsControl.IsGrouping" Value="True">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
                        </Trigger>
                        <Trigger Property="Window.AllowsTransparency" SourceName="Popup" Value="True">
                            <Setter Property="Border.CornerRadius" TargetName="DropDownBorder" Value="0"/>
                            <Setter Property="Border.BorderThickness" TargetName="DropDownBorder" Value="1,0,1,1"/>
                            <Setter Property="FrameworkElement.Margin" TargetName="DropDownBorder" Value="0,0,0,0"/>
                        </Trigger>
                        <Trigger Property="ComboBox.IsEditable" Value="True">
                            <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                            <Setter Property="UIElement.Visibility" TargetName="PART_EditableTextBox" Value="Visible"/>
                            <Setter Property="UIElement.Visibility" TargetName="ContentSite" Value="Hidden"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

这篇关于如何改变WPF组合框SelectedText背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆