Reputation: 885
I have a DataGrid
with the Foreground initialized on white, like so:
<DataGridTextColumn Header="February"
Binding="{Binding Path=Element[February].Value}"
Width="*"
Foreground="White" />
But when I try to set the foreground to black with a mouseover it's not working I don't know what I'm doing wrong and I could use some help, thank.
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="Black"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
EDIT: Full code
<Window x:Class="WPF_TEST.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="850
">
<Grid>
<DataGrid Margin="150,50,150,50" x:Name="GridBinding" ItemsSource="{Binding Path=Elements[Month]}" Background="Transparent" RowBackground="Transparent" AutoGenerateColumns="False" AreRowDetailsFrozen="True" SelectionUnit="Cell" GridLinesVisibility="None" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Oct." Binding="{Binding Path=Element[October].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Nov." Binding="{Binding Path=Element[November].Value}" Width="*" Foreground="White"/>
<DataGridTextColumn Header="Dec." Binding="{Binding Path=Element[December].Value}" Width="*" Foreground="White"/>
<DataGridTextColumn Header="Jan." Binding="{Binding Path=Element[January].Value}" Width="*" Foreground="White"/>
<DataGridTextColumn Header="Feb." Binding="{Binding Path=Element[February].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Mar." Binding="{Binding Path=Element[March].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Apr." Binding="{Binding Path=Element[April].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="May" Binding="{Binding Path=Element[May].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="June" Binding="{Binding Path=Element[June].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="July" Binding="{Binding Path=Element[July].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Aug." Binding="{Binding Path=Element[August].Value}" Width="*" Foreground="Black"/>
<DataGridTextColumn Header="Sep." Binding="{Binding Path=Element[September].Value}" Width="*" Foreground="Black"/>
</DataGrid.Columns>
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DataContext.Element[Depth].Value, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}}"/>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
</DataGrid>
</Grid>
<Window.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Foreground" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="BackgroundBorder"
BorderThickness="1">
<ContentPresenter VerticalAlignment="Center"
Margin="4,0,6,0" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Black" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
</Window>
Upvotes: 1
Views: 1132
Reputation: 22702
Try to move the StyleTrigger in the ControlTemplate
of DataGridCell
type. Instead of using white Foreground
color, use Transparent
:
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Foreground" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border x:Name="BackgroundBorder"
BorderThickness="1">
<ContentPresenter VerticalAlignment="Center"
Margin="4,0,6,0" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Black" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This Style
can be stored separately in resources, such as:
in Window.Resources
in DataGrid.Style
in App.xaml
- best place to store styles
Make sure that for the control not directly set the properties that must be set in the styles following, because local values have a higher priority than the setter styles. Read more here:
MSDN: Dependency Property Value Precedence
That is setting the local value like this:
<DataGridTextColumn Header="Oct."
Foreground="Black" />
You "circumcise" a Trigger
job and style setters.
Upvotes: 1