用于组合框模板的WPF高光笔刷

时间:2019-03-07 16:40:51

标签: wpf combobox styles datatemplate highlight

我不知道如何将“组合框”的“突出显示”画笔(将鼠标悬停在上面标记该项目的颜色)设置为特定的画笔。我使用了“编辑模板”->“复制”来获取模板的副本,在此尝试设置SystemColors.HighlightBrushKey。我已经看到了这样的答案:应该可以通过在样式资源中定义这样的笔刷来设置它,但是它根本不起作用。

我还尝试设置放置comobox的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>

1 个答案:

答案 0 :(得分:0)

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

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

更改悬停和选定的突出显示颜色

ItemContainerStyle="{DynamicResource MyComboBoxItemS}"

然后在XAML中,完整的Combobox看起来像这样

<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"/>