【问题标题】:How to change WPF Combo-Box style hover effect如何更改 WPF 组合框样式的悬停效果
【发布时间】:2019-06-17 01:36:51
【问题描述】:

我在 WPF 中创建了一个自定义组合框样式,但我认为这种令人讨厌的蓝色悬停效果与 FocusVisualStyle 有关,但我不确定。
我已尝试编辑背景、边框、鼠标悬停触发器和 FocusVisualStyle,但仍然没有。
自定义组合框样式

<Style x:Key="CB" TargetType="{x:Type ComboBox}">
            <Setter Property="Foreground" Value="Gray" />
            <Setter Property="BorderBrush" Value="#b3b3b3" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0 0 0 2"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="FrameworkElement.OverridesDefaultStyle" Value="True"/>
            <Setter Property="FontSize" Value="16" />
            <Setter Property="FontWeight" Value="Regular" />
            <Setter Property="MinWidth" Value="50"/>
            <Setter Property="MinHeight" Value="32"/>
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>

                            <!--ToggleButton-->
                            <ToggleButton Name="ToggleButton" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" BorderThickness="{TemplateBinding BorderThickness}"
                                          FocusVisualStyle="{x:Null}"
                                          Grid.Column="2"
                                          Focusable="false"
                                          IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
                                          ClickMode="Press">

                                <Border x:Name="templateRoot" SnapsToDevicePixels="true" Background="{x:Null}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="Transparent">
                                    <Border x:Name="splitBorder" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" SnapsToDevicePixels="true" Margin="200 0 0 0" HorizontalAlignment="Right" BorderThickness="1" BorderBrush="Transparent">
                                        <Path x:Name="arrow" VerticalAlignment="Center" Margin="0 0 0 0" HorizontalAlignment="Right" Fill="#333333" Data="M0,0 L0,2 L4,6 L8,2 L8,0 L4,4 z"/>
                                    </Border>
                                </Border>
                            </ToggleButton>

                            <ContentPresenter
                                Name="ContentSite"
                                IsHitTestVisible="False"
                                Content="{TemplateBinding SelectionBoxItem}"
                                ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                Margin="0 0 0 5"
                                FocusVisualStyle="{x:Null}"/>
                            <TextBox x:Name="PART_EditableTextBox"
                                Style="{x:Null}"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Margin="3,3,23,3"
                                Focusable="True"                               
                                Visibility="Hidden"
                                IsReadOnly="{TemplateBinding IsReadOnly}"/>
                            <Popup Name="Popup"
                                Placement="Bottom"
                                IsOpen="{TemplateBinding IsDropDownOpen}"
                                AllowsTransparency="True"
                                Focusable="False"
                                PopupAnimation="Slide"
                                FocusVisualStyle="{x:Null}">
                                <Grid Name="DropDown"
                                      SnapsToDevicePixels="True"               
                                      MinWidth="{TemplateBinding ActualWidth}"
                                      MaxHeight="{TemplateBinding MaxDropDownHeight}"
                                      FocusVisualStyle="{x:Null}">
                                    <Border x:Name="DropDownBorder"
                                            Background="White"
                                            BorderThickness="1.5"
                                            BorderBrush="{x:Null}"
                                            FocusVisualStyle="{x:Null}">
                                        <Border.Effect>
                                            <DropShadowEffect Opacity="0.6" ShadowDepth="3" BlurRadius="2"/>
                                        </Border.Effect>
                                    </Border>

                                    <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True" FocusVisualStyle="{x:Null}">
                                        <StackPanel FocusVisualStyle="{x:Null}" IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                    </ScrollViewer>
                                </Grid>
                            </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="True"/>
                            </Trigger>
                            <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                                <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="0 0 10 10"/>
                                <Setter TargetName="DropDownBorder" Property="Margin" Value="0,-5,0,0"/>
                            </Trigger>
                            <Trigger Property="IsEditable" Value="true">
                                <Setter Property="IsTabStop" Value="false"/>
                                <Setter TargetName="PART_EditableTextBox" Property="Visibility"  Value="Visible"/>
                                <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                            </Trigger>
                            <Trigger Property="UIElement.IsMouseOver" Value="True">
                                <Setter TargetName="arrow" Property="Fill" Value="White" />
                                <Setter Property="Background" Value="Transparent"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    您所描述的效果是ToggleButton 的鼠标悬停效果。您需要覆盖ToggleButtonControlTemplate 才能覆盖其默认行为:

    <!--ToggleButton-->
    <ToggleButton Name="ToggleButton" >
      <ToggleButton.Style>
        <Style TargetType="ToggleButton">
          <Setter Property="Background" 
                  Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}"/>
          <Setter Property="OverridesDefaultStyle" Value="True"/>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="ToggleButton">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Padding="{TemplateBinding Padding}">
                  <ContentPresenter />
                </Border>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </ToggleButton.Style>
    
      ...
    </ToggleButton>
    

    【讨论】:

    • 所以它是切换按钮样式?这对我来说非常有用,谢谢
    • @Draxalot2 不,ToggleButtonTemplate 是原因。默认模板定义触发器或视觉状态来处理基于状态变化(如鼠标悬停)的外观。该样式仅用于覆盖默认模板。
    • ComboBox 被选中的内容总是出现在最左边的位置。我试过改变 Padding 和 Margin,有什么问题?
    • @Draxalot2 在您的ComboBox 样式中,您有一个名为ContentSiteContentPresenter。修改其Margin 以操纵选定项的位置。
    • @Draxalot2 如果ComboBox 启用了编辑,则PART_EditableTextBox 的边距或填充优先于ContentPresenter 的设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 2020-11-16
    • 2018-09-23
    • 2012-11-25
    相关资源
    最近更新 更多