WPF覆盖DataGrid RowHeader模板并管理仍然选择行

时间:2011-04-04 18:52:16

标签: wpf datagrid header row styles

我已经覆盖默认的行标题样式以使用我的自定义按钮:

<Style x:Key="RowHeaderStyle" TargetType="DataGridRowHeader">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DataGridRowHeader">
                <Button HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Style="{StaticResource RowHeaderButton}" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

问题当然是单击按钮不再选择行。我是否必须在IsPressed事件上调用自定义方法?我怎么知道行索引?你会怎么做呢?

谢谢!

2 个答案:

答案 0 :(得分:1)

我最终只使用了造型,结果证明你可以做你想要的,如果不是那么激烈的话。如果有人设法获取代码来处理DataGridHeaderBorder上的行选择,请告诉我! 例如:

<...xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"...>

<BooleanToVisibilityConverter x:Key="bool2VisibilityConverter"/>

<LinearGradientBrush  x:Key="RowHeaderBackgroundBrush" EndPoint="0.728,0.5" StartPoint="0.272,0.5">
    <GradientStop Color="#FF494949" Offset="0"/>
    <GradientStop Color="#FF3E3E3E" Offset="1"/>
    <GradientStop Color="#FF494949" Offset="0.50"/>
    <GradientStop Color="#FF3E3E3E" Offset="0.50"/>
</LinearGradientBrush>

<LinearGradientBrush x:Key="RowHeaderBackgroundBrushMouseOver" EndPoint="0.728,0.5" StartPoint="0.272,0.5">
    <GradientStop Color="#FF666666" Offset="0"/>
    <GradientStop Color="#FF525252" Offset="1"/>
    <GradientStop Color="#FF666666" Offset="0.50"/>
    <GradientStop Color="#FF525252" Offset="0.50"/>
</LinearGradientBrush>

<Style x:Key="RowHeaderGripperStyle" TargetType="{x:Type Thumb}">
    <Setter Property="Height" Value="8"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Cursor" Value="SizeNS"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Thumb}">
                <Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="RowHeaderBorder" TargetType="Border">
    <Setter Property="Background" Value="{StaticResource RowHeaderBackgroundBrush}" />
    <Setter Property="BorderBrush" Value="#FF313131" />
    <Setter Property="BorderThickness" Value="0,0,1,1" />
    <Setter Property="CornerRadius" Value="0" />
    <Setter Property="Margin" Value="0" />

    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource RowHeaderBackgroundBrushMouseOver}"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

<Style x:Key="RowHeaderStyle1" TargetType="DataGridRowHeader">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DataGridRowHeader">
                <Grid>
                    <Microsoft_Windows_Themes:DataGridHeaderBorder IsPressed="{TemplateBinding IsPressed}" Orientation="Horizontal" SeparatorBrush="{TemplateBinding SeparatorBrush}" SeparatorVisibility="{TemplateBinding SeparatorVisibility}" Style="{StaticResource RowHeaderBorder}">
                        <StackPanel Orientation="Horizontal">
                            <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                            <Control SnapsToDevicePixels="false" Template="{Binding ValidationErrorTemplate, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}" Visibility="{Binding (Validation.HasError), Converter={StaticResource bool2VisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"/>
                        </StackPanel>
                    </Microsoft_Windows_Themes:DataGridHeaderBorder>
                    <Thumb x:Name="PART_TopHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Top"/>
                    <Thumb x:Name="PART_BottomHeaderGripper" Style="{StaticResource RowHeaderGripperStyle}" VerticalAlignment="Bottom"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案 1 :(得分:0)

是的,您必须编写自己选择的活动代码。

一种方法是使用Blend等工具获取默认模板的副本,只需修改其模板即可包含更改。