在ResourceDictionary中的Mouseover上更改Combobox / Table Item textcolor

时间:2016-09-26 14:08:39

标签: wpf vb.net combobox resourcedictionary

我正在使用MahAppsMetro,因此我有ResourceDictionary我可以更改设计的颜色。 现在我将颜色改为蓝色。但我不知道如何更改textcolor / selected itemcombobox的{​​{1}}。 它现在看起来像这样:

Example: Combobox

Example: Table

所以现在我想通过ResourceDictionary将Textcolor更改为白色。 它看起来像这样:

table

您能否告诉我需要添加到我的ResourceDictionary中,以便在项目的MouseOver上显示白色文本。

希望我很清楚我想做什么。另外请告诉我你需要的更多信息。

修改

<!-- 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" /> 现在正在运作。但不适用于Combobox。 以下是我添加DataGrid

的方法
DataGrid

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

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

我做错了什么?

2 个答案:

答案 0 :(得分:1)

这是可能的,但对于MouseOver,您需要覆盖MahApps ComboBoxItem并使用Trigger扩展它。 SelectedItem使用ColorBrush:AccentSelectedColorBrush。 这是使用Trigger for 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>

答案 1 :(得分:0)

对于DataGrid来说很简单:

如果您想为整个广告单元格设置样式,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以适用于整个应用程序。