用于Combobox模板的WPF Highlight画笔

问题描述 投票:0回答:1

我无法弄清楚如何将Combobox的Highlight画笔(将鼠标悬停时选中的项目标记的颜色)设置为某个画笔。我使用编辑模板 - >复制来获取模板的副本,我尝试设置SystemColors.HighlightBrushKey。我已经看到答案,应该能够通过在Style资源中定义它来设置画笔,但它只是不起作用。

我也尝试设置放置组合框的Grid Style.Resource,这也没有帮助。

请参阅第3行和第15行,我将颜色设置为红色。没有效果。我究竟做错了什么?

 <Style x:Key="TeachpendantVMVisionServerComboBoxStyle" TargetType="{x:Type ComboBox}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
        </Style.Resources>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <Grid x:Name="grid">
                        <Grid.Style>
                            <Style>
                                <Style.Resources>
                                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />
                                    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red" />
                                </Style.Resources>
                            </Style>
                        </Grid.Style>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition MaxWidth="18"/>
                        </Grid.ColumnDefinitions>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="ValidationStates">
                                <VisualState x:Name="Valid"/>
                                <VisualState x:Name="InvalidFocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="PART_EditableTextBox">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="Red"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill)" Storyboard.TargetName="path">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MoveTextBox.Invalid.BorderBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="toggleButton">
                                            <EasingColorKeyFrame KeyTime="0" Value="#FFFB0000"/>
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="InvalidUnfocused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="PART_EditableTextBox">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MoveTextBox.Invalid.BorderBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="toggleButton">
                                            <EasingColorKeyFrame KeyTime="0" Value="Red"/>
                                        </ColorAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Shape.Fill)" Storyboard.TargetName="path">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="Red"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <TextBox x:Name="PART_EditableTextBox"                             
                            Padding="5,0,0,0"                             
                            Height="{TemplateBinding Height}">
                            <TextBox.Template>
                                <ControlTemplate TargetType="{x:Type TextBox}">
                                    <Grid>
                                        <Border 
                                            x:Name="border"
                                            CornerRadius="3,0,0,3"                                        
                                            BorderThickness="1,1,0,1"
                                            Background="{DynamicResource Button.Static.Background}"
                                            BorderBrush="{DynamicResource Button.Static.Background}">
                                            <ScrollViewer x:Name="PART_ContentHost"/>
                                        </Border>
                                    </Grid>
                                </ControlTemplate>
                            </TextBox.Template>
                        </TextBox>
                        <ToggleButton x:Name="toggleButton" Grid.Column="1" Margin="0"                                      
                            Height="{TemplateBinding Height}"                            
                            Focusable="False"
                            IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                            ClickMode="Press" BorderBrush="#FFABADB3">
                            <ToggleButton.Template>
                                <ControlTemplate TargetType="{x:Type ToggleButton}">
                                    <Border                         
                                        Background="{DynamicResource Button.Checked.Background}" 
                                        x:Name="border" 
                                        CornerRadius="0,3,3,0" 
                                        BorderThickness="0,1,1,1"
                                        BorderBrush="{DynamicResource Button.Static.Background}">
                                        <ContentPresenter />
                                    </Border>
                                </ControlTemplate>
                            </ToggleButton.Template>
                            <Path x:Name="path" Grid.Column="1"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Data="M 0 0 L 4 4 L 8 0 Z"
                                Fill="Gold" />
                        </ToggleButton>
                        <ContentPresenter x:Name="ContentSite"
                            Content="{TemplateBinding SelectionBoxItem}"
                            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Right"
                            Margin="5,0,0,0" RecognizesAccessKey="True"/>
                        <Popup x:Name="Popup"
                            Placement="Bottom"
                            IsOpen="{TemplateBinding IsDropDownOpen}"
                            AllowsTransparency="True" 
                            Focusable="True"
                            PopupAnimation="Slide" 
                            OverridesDefaultStyle="True">
                            <Grid x:Name="DropDown"
                                SnapsToDevicePixels="True"                
                                MinWidth="{TemplateBinding ActualWidth}"
                                MaxHeight="{TemplateBinding MaxDropDownHeight}">                               
                                <Border 
                                    x:Name="DropDownBorder"
                                    BorderThickness="1"
                                    CornerRadius="5"
                                    Background="Azure"
                                    BorderBrush="DarkGray">                                    
                                </Border>   
                                <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">                                    
                                    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate/>
            </Setter.Value>
        </Setter>
    </Style>
wpf combobox styles datatemplate highlight
1个回答
0
投票

毕竟这很简单(但不明显在哪里找到它)。只需编辑ItemsContainerStyle的副本,就可以从那里设置所有这些画笔。

更改ComboBox的布局(创建圆形组合框或更改颜色)Style =“{DynamicResource MyComboBoxStyle}”

更改胡佛和选定的突出显示颜色

ItemContainerStyle="{DynamicResource MyComboBoxItemS}"

完整的Combobox在XAML中看起来就像这样

<ComboBox x:Name="positionsComboBox"
   Grid.Column="1" 
   Grid.ColumnSpan="3"
   IsReadOnly = "True"
   IsEditable = "False"                                       
   Margin="3,0,3,0"
   ItemsSource="{Binding IDs, Source={StaticResource Locator}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"                                 
   SelectedValue="{Binding SelectedID, Source={StaticResource Locator}, Mode=TwoWay}"                                                            
   Style="{DynamicResource MyComboBoxStyle}"
   IsSynchronizedWithCurrentItem="True"                                 
   ItemContainerStyle="{DynamicResource MyComboBoxItemContainerStyle}"                                                                           
   FontWeight="Bold" 
   FontSize="24"/>  
© www.soinside.com 2019 - 2024. All rights reserved.