开发者

WPF override DataGrid RowHeader template and manage to select rows still

I have overriden the default row header style to use my custom button:

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

The problem of course is that clicking the button no longer selects the row. Will I have to call a custom m开发者_如何学运维ethod on the IsPressed event? How would I know the row index? How would you go about doing it?

Thanks!


I've ended up using only styling which turns out you can probably do what you want if it isn't drastic. If anyone manages to get the code behind to handle row selection on DataGridHeaderBorder please let me know! Example:

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


Yes you will have to code your own selected event.

One way to do that is to get a copy of the default template using a tool like Blend and just modify their template to include your changes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜