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));
}
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)); }
The AttachedProperty gives us something to work with, when we are building in our own logic to a UI. The only safe and available alternative is the Tag property, which is a lazy alternative and not 'clean code', as far as readability is concerned.
<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.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>
