Reputation: 459
I have DataGrid:
<DataGrid x:Name="dgNames">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="tcContent" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Top" Text="Content"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Text}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
How can I set style (hand cursor) for first row only ?
Upvotes: 0
Views: 957
Reputation: 9944
You can achieve that using a Converter to get the Row index, then set the cursor based on that index in a DataTrigger
:
<Window ...>
<Window.Resources>
<local:RowIndexConverter x:Key="RowIndexConverter"/>
</Window.Resources>
<Grid>
<DataGrid x:Name="dgNames" ItemsSource="{Binding DgCollection}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn x:Name="tcContent" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock VerticalAlignment="Top" Text="Content"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Text}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGridRow}},
Converter={StaticResource RowIndexConverter}}"
Value="0">
<Setter Property="Cursor" Value="Hand"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
The RowIndexConverter
returns the current Row index:
public class RowIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
return (value as DataGridRow).GetIndex();
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Note that DgCollection
is a simple ObservableCollection
.
Upvotes: 2