WPF Toolkit Datagrid,为SelectorAll设置样式

时间:2012-02-09 09:43:35

标签: c# wpf xaml datagrid wpftoolkit

我想设置此Datagrid的左上角元素,它选择所有行,但我不知道如何。有没有人有我的例子,或者有谁知道我需要设计哪个元素?

我的Datagrid来自WPF工具包btw。

enter image description here

1 个答案:

答案 0 :(得分:2)

首先,我建议您更新到.net 4.0,以便能够使用WPF dataGrid,而无需安装WPFToolkit。

现在关于你的问题,你必须为你的数据网格设置一个样式,并在样式,模板或资源中放置这种代码:

    <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>

(这是我的一些代码的示例,我放在MyDataGrid.Resources中,MyDataGrid是从DataGrid派生的类)

编辑:重要部分当然是按钮样式声明器中的x:Key="{ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly={x:Type DataGrid}}"