DataGridCell IsSelected in Trigger

Sep 9, 2008 at 3:33 PM
Edited Sep 9, 2008 at 4:32 PM
Hi,

I've defined a celltemplate that contains some elements (border , grid, textblock)
The external border element name is ExternalBorder.
I need to change the BorderBrush color of ExternalBorder when the DataGridCell is Selected
I've tried with a DataTrigger in the DataTemplate with no success. The problem is that I don't know how to reference the DataGridCell.

I've tried with
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type dg:DataGridCell}, AncestorLevel=1}, Path=IsSelected}" Value="True">
    <Setter TargetName="ExternalBorder" Property="BorderBrush" Value="Gold" />
</DataTrigger>

and with:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" Value="True">
    <Setter TargetName="ExternalBorder" Property="BorderBrush" Value="Red" />
</DataTrigger>

At runtime nothing happens even if I change the DataTrigger value to False. I can't understand if the problem is in the DataTrigger Binding definition or in the Setter markup.

Thanks
Marco

Example Code (you don't need to implement databinding):
<Window x:Class="SitoGroup.Fast.Sbk.Wpf.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    Title="Window1" Height="300" Width="300">
   
    <Window.Resources>
        <DataTemplate x:Key="dt1" DataType="{x:Type dg:DataGridCell}">
            <Border Name="ExternalBorder" BorderBrush="Gold" BorderThickness="3">
                <TextBlock></TextBlock>
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" Value="True">
                    <Setter TargetName="ExternalBorder" Property="BorderBrush" Value="Red" />
                    <Setter TargetName="ExternalBorder" Property="BorderThickness" Value="3" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <Style x:Key="CellStyle" TargetType="{x:Type dg:DataGridCell}">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Gold" />
                    <Setter Property="BorderThickness" Value="0" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <dg:DataGrid Name="dataGrid1" SelectionUnit="Cell" AutoGenerateColumns="False">
            <dg:DataGrid.Columns>
                <dg:DataGridTemplateColumn CellTemplate="{StaticResource dt1}" CellStyle="{StaticResource CellStyle}" x:Name="c1" />
                <dg:DataGridTemplateColumn CellTemplate="{StaticResource dt1}" CellStyle="{StaticResource CellStyle}" x:Name="c2" />
                <dg:DataGridTemplateColumn CellTemplate="{StaticResource dt1}" CellStyle="{StaticResource CellStyle}" x:Name="c3" />
                <dg:DataGridTemplateColumn CellTemplate="{StaticResource dt1}" CellStyle="{StaticResource CellStyle}" x:Name="c4" />
            </dg:DataGrid.Columns>
        </dg:DataGrid>
    </Grid>
</Window>

 

Coordinator
Sep 9, 2008 at 5:43 PM
This is a known issue and will be address in the v1 release.