Reputation: 5885
I want to style the upper left element of this Datagrid that selects all rows but I don't know how. Does anyone have an example for me or does anyone know which element I need to style ?
My Datagrid comes from the WPF Toolkit btw.
Upvotes: 1
Views: 1199
Reputation: 6124
first of all, I'd advise you to update to .net 4.0 to be able to use the WPF dataGrid without resorting to installing the WPFToolkit.
now as for your issue, you have to set a style to your datagrid, and inside the style, template or resource put this kind of code:
<Style TargetType="{x:Type Button}" x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}">
<EventSetter Event="PreviewMouseDown" Handler="SelectAllButtonPreviewMouseDownHandler" />
<EventSetter Event="PreviewMouseUp" Handler="SelectAllButtonPreviewMouseUpHandler" />
<Setter Property="ToolTip" Value="{Binding SelectAllButtonToolTip, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
<Setter Property="Content" Value="{Binding SelectAllButtonContent, RelativeSource={RelativeSource AncestorType=DataGrid}}" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Background="Transparent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Polygon Name="Arrow"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Margin="0,0,3,3"
Points="0,9 9,9 9,0"
Fill="Black"
Opacity="0.15"
Visibility="Collapsed"/>
<ContentPresenter Name="ContentSite" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Content" Value="{x:Null}">
<Setter TargetName="ContentSite" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="Arrow" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Arrow" Property="Opacity" Value="0.75"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
(this is a sample from some code of mine, that I put in MyDataGrid.Resources, MyDataGrid being a class derivated from DataGrid)
edit: the important part is of course the x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}"
in the button's style declarator.
Upvotes: 2