LM_DMKHNGST LM_DMKHNGST - 2 months ago 21
Vb.net Question

Changing Combobox/Table Item textcolor on Mouseover in ResourceDictionary

I am using

MahAppsMetro
and therefore I have a
ResourceDictionary
where I can change the color of the design.
Now I changed the color to blue. But I do not know how to change the
textcolor
of an
selected item
in an
combobox
/
table
.
It looks like this now:

Example: Combobox

Example: Table

So now I want to change the Textcolor to white by the ResourceDictionary..
It looks something like this yet:

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


Can you tell me what I need to add to my ResourceDictionary to have a white text on MouseOver of the items.

Hope it is clear what I want to do. Else please tell me what further information you need.

Answer

it is possible, but for the MouseOver you need to override MahApps ComboBoxItem and extend it with a Trigger. The SelectedItem uses the ColorBrush: AccentSelectedColorBrush. Here is the original MetroComboBoxItem extended with a Trigger for MouseOver

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>