Reputation: 149
I created DataGrid and put background to bi transparent. Now i want to set foreground to be Blue but it does`t want to change, always is black. I try to put Foreground="Blue" in all those places but never worked.
<DataGrid Background="Transparent" Foreground="Blue">
<DataGrid.Columns Foreground="Blue">
<DataGridTextColumn Foreground="Blue" Header="AAA"/>
<DataGridTextColumn Foreground="Blue" Header="BBB"/>
<DataGridTextColumn Foreground="Blue" Header="CCC"/>
<DataGridTextColumn Foreground="Blue" Header="DDD"/>
</DataGrid.Columns>
</DataGrid>
I have this in my App.xaml so maybe there is some Themes that affect it but i don`t know how to overwrite it.
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Indigo.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Upvotes: 0
Views: 500
Reputation: 149
Fore some reason that don`t work in my project, there is some theme applied and overwrite wherever i put Foreground. What i did is put in DataGridTextColumn.Header Label and sat Foreground of Label and that worked. And i did it for every DataGridTextColumn in my DataGrid
<DataGridTextColumn Binding="{Binding Name}" IsReadOnly="True" Width="Auto" >
<DataGridTextColumn.Header>
<Label Foreground="#FF0398E2" FontWeight="Bold" FontSize="16">Name</Label>
</DataGridTextColumn.Header>
</DataGridTextColumn>
Upvotes: 1
Reputation: 3312
You need to add some data to the datagrid to see what your code is doing:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<XmlDataProvider x:Key="MockList" XPath="/MockObjects/*" >
<x:XData >
<MockObjects xmlns="">
<MockObject Name="Louis" Type="TTTT" Number="1" />
<MockObject Name="Joseph" Type="TTTT" Number="2" />
<MockObject Name="Papineau" Type="ZZZZ" Number="3" />
</MockObjects>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource MockList}}">
<DataGrid Background="Transparent" Name="MainDataGrid"
ItemsSource="{Binding Mode=Default, XPath=/MockObjects/MockObject}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Foreground="Blue" Header="AAA" Binding="{Binding XPath=@Name}" />
<DataGridTextColumn Foreground="Blue" Header="BBB" Binding="{Binding XPath=@Type}"/>
<DataGridTextColumn Foreground="Blue" Header="CCC" Binding="{Binding XPath=@Number}"/>
<DataGridTextColumn Foreground="Blue" Header="DDD"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
It seems that DataGridTextColumn Foreground sets the foreground of the data, not the header.
The header seems to inherit its Foreground from DataGrid.Foreground. Make the following change in my code:
<DataGrid Background="Transparent" Name="MainDataGrid" Foreground="Blue"
To investigate how the value inheritance works (or if the value does come from a template), set a breakpoint in your program:
Run the program and double click on the datagrid. Quickwatch MainDataGrid and open the WPF Tree Visualiser
The TextBlock is displaying the column header text. It inherits Foreground blue from all the lines marked yellow. Only the MainDataGrid does not inherit the value, but is set locally in XAML. The Treeview gives you this information if you click on MainDataGrid Foreground.
Note: This line in your code does not compile
<DataGrid.Columns Foreground="Blue">
DataGrid.Columns doesn't have a Foreground property.
It's well know that getting the formatting of the WPF DataGrid to work properly is a major headache. I recommend you read my article on codeproject Guide to WPF DataGrid Formatting Using Bindings to see samples for all kind of datagrid formatting.
Upvotes: 1
Reputation: 12276
The problem is that there's a template being applied to each of the cells and wpf doesn't always inherit down into template generated ui.
You could try putting this in your datagrid:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="TextElement.Foreground" Value="Blue" />
</Style>
</DataGrid.CellStyle>
Upvotes: 1