0

I created own custom attached property on DataGridColumn - utilities:ADPs.IsCellSelected to indicate whether any cell is selected in the column. I did similar for rows and it works fine. Now I want to use this property to change the whole column e.g. background property like:

         <DataGridTemplateColumn x:Name="MyColumn" Header="Test">
         <DataGridTemplateColumn.CellStyle>
            <Style TargetType="DataGridCell" BasedOn="{StaticResource BaseCell}">
               <Setter Property="Background" Value="Black"/>
            </Style>
         </DataGridTemplateColumn.CellStyle>

         <DataGridTemplateColumn.CellTemplate>
            <DataTemplate x:Uid="DataTemplate_3">
               <TextBlock x:Name="trg" Text="some text" />
               <DataTemplate.Triggers>
                  <Trigger SourceName="MyColumn" Property="utilities:ADPs.IsCellSelectedColumn" Value="True">
                     <Setter TargetName="trg" Property="Background" Value="Red"/>
                  </Trigger>
               </DataTemplate.Triggers>
            </DataTemplate>
         </DataGridTemplateColumn.CellTemplate>
         </DataGridTemplateColumn>

But VS shows an error "The name MyColumn is not recognized.". Any idea how I can access to my new attached DataGridColumn property from "cells"?.

EDIT: Probably i ti sno t possible what I intend because: "DataTemplate has its own NameScope, therefore you cannot refer to an element outside DataTemplate using ElementName. Moreover, DataGridTemplateColumn is not an UIElement and is not in VisualTree, therefore DataGridTemplateColumn is not parent of your elements defined in DataTemplate. DataGridTemplateColumn is used just to define columns. DataGrid then generate headers, rows and cells based on the definitions, but the DataGridColumns never gets rendered." link

Marek S.
  • 11
  • 1
  • 2

1 Answers1

0

Instead to change Background property of whole column with selected cell, I change only column header Background property. I use my custom attached property on DataGridColumnHeader - utilities:ADPs.IsCellSelected and then highligth the header

<DataGridTemplateColumn.HeaderStyle>
   <Style TargetType="DataGridColumnHeader">
      <Style.Triggers>
         <Trigger Property="utilities:ADPs.IsCellSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
         </Trigger>
      </Style.Triggers>
   </Style>
</DataGridTemplateColumn.HeaderStyle>
Marek S.
  • 11
  • 1
  • 2