WPF 覆盖 DataGrid RowHeader 模板并仍然设法选择行

发布于 2024-10-30 01:30:14 字数 578 浏览 0 评论 0原文

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

<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 事件上调用自定义方法?我如何知道行索引?你会怎样做呢?

谢谢!

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 method on the IsPressed event? How would I know the row index? How would you go about doing it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沒落の蓅哖 2024-11-06 01:30:14

我最终只使用了样式,事实证明,如果不是太激烈的话,你可能可以做你想做的事。如果有人设法获取后面的代码来处理 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>

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>
兔姬 2024-11-06 01:30:14

是的,您必须对自己选择的事件进行编码。

一种方法是使用 Blend 等工具获取默认模板的副本,然后修改其模板以包含您的更改。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文