locked
Set the RowHeaderStyle when a single cell is selected RRS feed

  • Question

  • I am working on a WPF project, and I am using DataGrids.

    Since I need the RowHeader Style to change when the DataGridCell is selected I do the following:

    <DataGrid Margin="6,25,6,35" x:Name="myDataGrid" >
                         <DataGrid.CellStyle>
                 <Style TargetType="{x:Type DataGridCell}">
                     <Style.Triggers>
                          <Trigger Property="IsSelected" Value="True" >
                             <Setter Property="DataGrid.RowHeaderStyle" Value="{StaticResource TheStyleForTheRowHeader}" />
                         </Trigger>
                     </Style.Triggers>
                 </Style>
            </DataGrid.CellStyle>
     </DataGrid>

    So, I have a static resource which is a style for the RowHeaderStyle property, but when I run the program and select a cell the RowHeader does not changes.

    What am I doing wrong??

    Hope someone can help me. Thanks in advance.


    Thursday, July 12, 2012 6:44 PM

Answers

  • I am unsure what exactly you are trying to do. 

    If I understand correctly, to set the DataGrid.RowHeaderStyle will change ALL the rows' headers to the new style?

     

    Did you mean you wanted to change the RowHeaderStyle only for the row of the selected cell? AFAIK you can't.

    Also, I don't think you can traverse back up and make a property change to a parent, from a property trigger's setter like that.

    If someone proves me wrong, the rest is not worth reading ;)

      

    Changing the style means changing a property, or the template.

    This is best done by a Trigger, up where it needs to be done.

    To achieve this my first choice would be an AttachedProperty.

     

    Below are three examples of triggers.

    1) Changing the row header template

    2) Changing a property for the row header

    3) Changing DataGrid.RowHeaderStyle as you suggested

     

    Start new project "WpfApplication71" for cut and paste ease...

    MainWindow.xaml

    <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            x:Class="WpfApplication71.MainWindow"
            Title="MainWindow" 
            xmlns:local="clr-namespace:WpfApplication71">
    
        <Window.Resources>
            <ControlTemplate x:Key="DifferentHeaderTemplate" TargetType="{x:Type DataGridRowHeader}">
                <Grid Background="Red" Width="10" />
            </ControlTemplate>
            
            <Style x:Key="DataGridRowHeaderStyle1" TargetType="{x:Type DataGridRowHeader}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding (local:AttachedProperties.IsFocussed), Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}" Value="true">
                        <Setter Property="Template" Value="{DynamicResource DifferentHeaderTemplate}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
    
            <Style x:Key="DataGridRowHeaderStyle2" TargetType="{x:Type DataGridRowHeader}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding (local:AttachedProperties.IsFocussed), Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}" Value="true">
                        <Setter Property="Background" Value="Red" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
    
            <Style x:Key="DataGridRowHeaderStyle3" TargetType="{x:Type DataGridRowHeader}">
                <Setter Property="Template" Value="{DynamicResource DifferentHeaderTemplate}" />
            </Style>
        </Window.Resources>
        
        <StackPanel>
    
            <!-- Changing DataGridRowHeader ControlTemplate for selected cell -->
            <DataGrid Margin="10" x:Name="myDataGrid1" ItemsSource="{Binding AllItems}" RowHeaderStyle="{DynamicResource DataGridRowHeaderStyle1}" >
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="IsSelected" Value="{Binding (local:AttachedProperties.IsFocussed), Mode=OneWayToSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}"/>
                    </Style>
                </DataGrid.CellStyle>
            </DataGrid>
    
            <!-- Changing DataGridRowHeader Properties for selected cell -->
            <DataGrid Margin="10" x:Name="myDataGrid2" ItemsSource="{Binding AllItems}" RowHeaderStyle="{DynamicResource DataGridRowHeaderStyle2}" >
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="IsSelected" Value="{Binding (local:AttachedProperties.IsFocussed), Mode=OneWayToSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}"/>
                    </Style>
                </DataGrid.CellStyle>
            </DataGrid>
            
            <!-- Changing DataGrid.RowHeaderStyle -->
            <DataGrid Margin="10" x:Name="myDataGrid3" ItemsSource="{Binding AllItems}" >
                <DataGrid.Style>
                    <Style TargetType="DataGrid">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding (local:AttachedProperties.IsFocussed), Mode=OneWay, RelativeSource={RelativeSource Self}}" Value="true">
                                <Setter Property="DataGrid.RowHeaderStyle" Value="{DynamicResource DataGridRowHeaderStyle3}" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.Style>
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="IsSelected" Value="{Binding (local:AttachedProperties.IsFocussed), Mode=OneWayToSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}}"/>
                    </Style>
                </DataGrid.CellStyle>
            </DataGrid>
        </StackPanel>
    
    </Window>

    MainWindow.xaml.cs

    using System.Windows;
    using System.Collections.ObjectModel;
    
    namespace WpfApplication71
    {
        public partial class MainWindow : Window
        {
            public ObservableCollection<AnItem> AllItems { get; set; }
    
            public MainWindow()
            {
                InitializeComponent();
                AllItems = new ObservableCollection<AnItem>();
                AllItems.Add(new AnItem("Item 01", "I am an item 1"));
                AllItems.Add(new AnItem("Item 02", "I am an item 2"));
                DataContext = this;
            }
    
        }
    
    }

    AttachedProperties.cs

    using System.Windows;
    
    namespace WpfApplication71
    {
        public class AttachedProperties
        {
            public static bool GetIsFocussed(DependencyObject obj)
            {
                return (bool)obj.GetValue(IsFocussedProperty);
            }
    
            public static void SetIsFocussed(DependencyObject obj, bool value)
            {
                obj.SetValue(IsFocussedProperty, value);
            }
    
            public static readonly DependencyProperty IsFocussedProperty =
                DependencyProperty.RegisterAttached("IsFocussed", typeof(bool), typeof(AttachedProperties), new UIPropertyMetadata(false));
        }
    }

     

    AnItem.cs

    namespace WpfApplication71
    {
        public class AnItem
        {
            public string Name { get; set; }
            public string Description { get; set; }
    
            public AnItem(string name, string description)
            {
                Name = name;
                Description = description;
            }
        }
    }
     

    Regards,
    Pete


    #PEJL



    Thursday, July 12, 2012 10:29 PM