更改ComboBox边框颜色 [英] Change ComboBox border color

查看:181
本文介绍了更改ComboBox边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试更改我的 ComboBox 边框颜色。

I try to change my ComboBox border color.

这是 我正在使用:

<Style x:Key="ComboBoxStyle1" 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="TextElement.Foreground" Value="Black"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <Grid>
                    <ToggleButton x:Name="ToggleButton" Grid.Column="2"
                        ClickMode="Press" Focusable="False"
                        IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                        Template="{StaticResource ComboBoxToggleButtonTemplate}"/>

                    <ContentPresenter x:Name="ContentSite" Margin="5, 3, 23, 3" IsHitTestVisible="False"
                        HorizontalAlignment="Left" VerticalAlignment="Center"                              
                        Content="{TemplateBinding SelectionBoxItem}" 
                        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/>

                    <TextBox x:Name="PART_EditableTextBox" Margin="3, 3, 23, 3"                     
                        IsReadOnly="{TemplateBinding IsReadOnly}"
                        Visibility="Hidden" Background="Transparent"
                        HorizontalAlignment="Left" VerticalAlignment="Center"
                        Focusable="True" >
                        <TextBox.Template>
                            <ControlTemplate TargetType="{x:Type TextBox}" >
                                <Border x:Name="PART_ContentHost" Focusable="False" />
                            </ControlTemplate>
                        </TextBox.Template>
                    </TextBox>
                    <!-- Popup showing items -->
                    <Popup x:Name="Popup" Placement="Bottom"
                        Focusable="False" AllowsTransparency="True"
                        IsOpen="{TemplateBinding IsDropDownOpen}"
                        PopupAnimation="Slide" >
                        <Grid x:Name="DropDown" SnapsToDevicePixels="True"
                            MinWidth="{TemplateBinding ActualWidth}"
                            MaxHeight="{TemplateBinding MaxDropDownHeight}">
                            <Border x:Name="DropDownBorder" Background="Transparent" Margin="0, 1, 0, 0"
                                CornerRadius="0" BorderThickness="1,1,1,1" 
                                BorderBrush="{StaticResource ComboBoxNormalBorderBrush}"/>
                            <ScrollViewer Margin="4" SnapsToDevicePixels="True">
                                <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="False">
                        <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="TextElement.Foreground" Value="{StaticResource ComboBoxDisabledForegroundBrush}"/>
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="True">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="False"/>
                    </Trigger>
                    <Trigger Property="IsEditable" Value="True">
                        <Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
                        <Setter Property="Visibility" TargetName="PART_EditableTextBox" Value="Visible"/>
                        <Setter Property="Visibility" TargetName="ContentSite" Value="Hidden"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我尝试更改所有属性,但仍然是 ComboBox 边框颜色是白色

I try to change all the properties but still my ComboBox border color is white:

我也尝试定义 ComboBox 控制器或通过后面的代码。

I also try to define the color inside my ComboBox controller or via code behind.

更新

此代码:

<Border BorderBrush="Red" BorderThickness="3" /> 

这是结果:

如您所见仍然具有我想要删除的白色颜色。

As you can see i still have the white color that i want to remove.

推荐答案

可以将您的ControlTemplate的根目录添加到Border:

You can add your which is the root of your ControlTemplate to Border:

<ControlTemplate TargetType="{x:Type ComboBox}">
    <Border BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}">
        <Grid>
            <!--your template is here-->
        </Grid>
    </Border>

    <!--etc-->

这篇关于更改ComboBox边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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