在 ResourceDictionary 中鼠标悬停时更改 Combobox/Table 项目文本颜色

Changing Combobox/Table Item textcolor on Mouseover in ResourceDictionary

我正在使用 MahAppsMetro,因此我有一个 ResourceDictionary,我可以在其中更改设计的颜色。 现在我将颜色更改为蓝色。但是我不知道如何在 combobox / table 中更改 selected itemtextcolor。 现在看起来像这样:

Example: Combobox

Example: Table

所以现在我想通过 ResourceDictionary 将文本颜色更改为白色.. 它看起来像这样:

<!-- re-set brushes too -->
<SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HighlightColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush2" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush3" Color="{StaticResource AccentColor3}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush4" Color="{StaticResource AccentColor4}" options:Freeze="True" />

<SolidColorBrush x:Key="WindowTitleColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />

<LinearGradientBrush x:Key="ProgressBrush" EndPoint="0.001,0.5" StartPoint="1.002,0.5" options:Freeze="True">
    <GradientStop Color="{StaticResource HighlightColor}" Offset="0" />
    <GradientStop Color="{StaticResource AccentColor3}" Offset="1" />
</LinearGradientBrush>

<SolidColorBrush x:Key="CheckmarkFill" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="RightArrowFill" Color="{StaticResource AccentColor}" options:Freeze="True" />

你能告诉我我需要向我的 ResourceDictionary 添加什么才能在项目的 MouseOver 上显示白色文本吗?

希望清楚我想做什么。否则请告诉我您需要什么进一步的信息。

编辑:

对于Combobox,它现在可以工作了吗?但不适用于 DataGrid。 这是我添加 DataGrid:

的方式
 <DataGrid x:Name="mydatagrid" SelectionUnit="FullRow" HorizontalAlignment="Left" Margin="159,33,0,0" VerticalAlignment="Top" Height="204" Width="275" Background="{x:Null}" BorderBrush="Transparent" CanUserAddRows="False" GridLinesVisibility="None" BorderThickness="0" CanUserResizeRows="False">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="User" Binding="{Binding User, Mode=OneWay}" Width="100" IsReadOnly="True" CanUserResize="False" />
                        <DataGridTextColumn Header="Vote" Binding="{Binding Vote, Mode=OneWay}" Width="90" IsReadOnly="True" CanUserResize="False"  />
                        <DataGridTemplateColumn Header="Status" Width="66" IsReadOnly="True" CanUserResize="False">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Image Source="{Binding Status, Mode=OneWay}" Width="16"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTextColumn Header="Real_Username" Binding="{Binding Real_Username, Mode=OneWay}" Width="90" IsReadOnly="True" CanUserResize="False"/>
                    </DataGrid.Columns>
                </DataGrid>

我尝试向我的 Application.xaml 添加不同的方法,并扩展了 IsMouseOver 的触发器:

<Setter Property="Controls.Foreground" Value="{DynamicResource AccentSelectedColorBrush}" />

我做错了什么?

这是可能的,但对于 MouseOver,您需要重写 MahApps ComboBoxItem 并使用触发器对其进行扩展。 SelectedItem 使用 ColorBrush:AccentSelectedColorBrush。 这是使用 MouseOver

的触发器扩展的原始 MetroComboBoxItem

app.xaml

<Application x:Class="WpfApplication.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
 <ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
    <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
    <!-- Accent and AppTheme setting -->
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
  </ResourceDictionary.MergedDictionaries>

  <SolidColorBrush x:Key="AccentSelectedColorBrush" Color="DeepPink" />
  <Style TargetType="ComboBoxItem" x:Key="MetroComboBoxItem">
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
    <Setter Property="Padding" Value="2" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="Background" Value="{DynamicResource TransparentWhiteBrush}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBoxItem">
                <Grid Background="{TemplateBinding Background}" Margin="0,0.5">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                   Storyboard.TargetName="MouseOverRectangle">
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.1"
                                                              Value=".65" />
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity"
                                                                   Storyboard.TargetName="contentPresenter">
                                        <SplineDoubleKeyFrame KeyTime="0"
                                                              Value=".55" />
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Unselected" />
                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)"
                                                                   Storyboard.TargetName="SelectedRectangle">
                                        <EasingDoubleKeyFrame KeyTime="0:0:0.1"
                                                              Value="1" />
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused">
                                <Storyboard />
                            </VisualState>
                            <VisualState x:Name="Unfocused" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Rectangle x:Name="SelectedRectangle"
                               IsHitTestVisible="False"
                               Opacity="0"
                               Fill="{DynamicResource AccentColorBrush}" />
                    <Rectangle x:Name="MouseOverRectangle"
                               IsHitTestVisible="False"
                               Opacity="0"
                               Fill="{DynamicResource AccentColorBrush3}" />
                    <ContentControl Foreground="{TemplateBinding Foreground}">
                        <ContentPresenter x:Name="contentPresenter"
                                          Margin="{TemplateBinding Padding}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" />
                    </ContentControl>
                    <Rectangle x:Name="FocusVisualElement"
                               Stroke="{DynamicResource HighlightBrush}"
                               StrokeThickness="1"
                               Visibility="Collapsed" />
                </Grid>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Foreground" Value="{DynamicResource AccentSelectedColorBrush}" />
                    </Trigger>
                    <!-- This Trigger is new -->
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="{DynamicResource AccentSelectedColorBrush}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
      </Setter>
     </Style>
    </ResourceDictionary>
  </Application.Resources>
</Application>

对于 DataGrid 来说很简单:

如果您想为整个 Line 单元格设置样式,OnMouseOverIsSelected 使用此:

<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
    <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <!-- Full Row -->
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}, Path=IsMouseOver}" Value="True">
                <Setter Property="Foreground" Value="Red" />
                <Setter Property="Opacity" Value="0.8" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}, Path=IsSelected}" Value="True">
                <Setter Property="Foreground" Value="DeepPink" />
                <Setter Property="Opacity" Value="0.3" />
            </DataTrigger>
        </Style.Triggers>
</Style>

只有这个单元格:

<Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
    <Setter Property="Foreground" Value="Green" />
        <Style.Triggers>
            <!-- Cell only -->
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True">
                <Setter Property="Foreground" Value="Red" />
                    <Setter Property="Opacity" Value="0.8" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="True">
                <Setter Property="Foreground" Value="DeepPink" />
                <Setter Property="Opacity" Value="0.3" />
            </DataTrigger>
        </Style.Triggers>
</Style>

您可以将 Style 放在您的 app.xaml 中以用于您的整个应用程序。