C# WPF DataGrid column header style error Cannot find source: RelativeSource FindAncestor

Mojtaba_Hakim 281 Reputation points
2023-11-07T02:04:56.93+00:00

I'm using C# WPF .NET Core 6 also Material Design Theme nuget, I have problem with DataGrid's Column Header Style

I have Set a Style for Column Headers of my DataGrid

App.xaml :

  <Application.Resources>
      <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.LightBlue.xaml" />
              <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.LightBlue.xaml" />
              <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.LightBlue.xaml" />
              <ResourceDictionary Source="pack://application:,,,/UiDictionary/Greeny.xaml" />
          </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
  </Application.Resources>

Greeny.xaml:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    x:Name="GreenyTheme">
 <Style x:Key="CenteryHeaderstyle" TargetType="DataGridColumnHeader" BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}">
     <Setter Property="HorizontalContentAlignment" Value="Center"/>
     <Setter Property="FontWeight" Value="Bold"/>
     <Setter Property="FontSize" Value="13"/>
     <Setter Property="Height" Value="44"/>
 </Style>
</ResourceDictionary>

MyWindow.xaml :

<DataGrid   x:Name="INVO_LST_sub"
        ColumnHeaderStyle="{StaticResource CenteryHeaderstyle}"     
        SelectedIndex="0"
        KeyboardNavigation.TabNavigation="Contained"
        ItemsSource="{Binding FACTOR22_INVO_DATA}"
        IsKeyboardFocusWithinChanged="INVO_LST_sub_IsKeyboardFocusWithinChanged"     
        Margin="3,241,6,5" AutoGenerateColumns="False"  FlowDirection="RightToLeft"  SelectionUnit="FullRow"   BorderThickness="1">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="NAME_CODE_COLUMN" Binding="{Binding NAME_CODE,UpdateSourceTrigger=LostFocus}" Header="NAME" Width="auto" MinWidth="50" />
            <DataGridComboBoxColumn x:Name="VAHED_K_COLUMN"  
                Width="auto"   MinWidth="90" Header=" واحد کالا " Visibility="Visible" 
                                    SelectedValueBinding="{Binding VAHED_K, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="NAMES" SelectedValuePath="VAHED">
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="IsEditable" Value="True"/>
                        <Setter Property="VirtualizingPanel.IsVirtualizing" Value="True"/>
                        <Setter Property="VirtualizingPanel.VirtualizationMode" Value="Recycling"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>
        </DataGrid.Columns>
    </DataGrid>

When I'm trying to enter some data into my DataGrid suddenly I get this error in my XAML (Binding failures) :

HorizontalGridLinesBrush DataGridColumnHeader.BorderBrush Brush Cannot find source: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1'.

GridLinesVisibility DataGridColumnHeader.BorderThickness Thickness Cannot find source: RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1'.

123

After a lot of Debugging I found out if I remove this BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}" from My Style <Style x:Key="CenteryHeaderstyle" TargetType="DataGridColumnHeader" BasedOn="{StaticResource MaterialDesignDataGridColumnHeader}">

It would be fixed !

The point is I want to keep the default Style of Material Design for my DataGrid but change some things

How do I fix those errors ?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,385 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,262 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
765 questions
{count} votes