在WPF中为DataGridRow创建ControlTemplate [英] Creating a ControlTemplate for a DataGridRow in WPF

查看:115
本文介绍了在WPF中为DataGridRow创建ControlTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要完成的工作是自定义DataGrid控件,以便每行都有圆角,没有网格线(仅是我正在使用的设计).

What I am trying to accomplish is customize the DataGrid control so that each row has rounded corners, no gridlines (just the design I'm working with).

我一直想做的是创建一个ControlTemplate来修改DataGridRow控件,以使它们具有预期的外观.到目前为止,这是我正在使用的:

What I have been trying to do is create a ControlTemplate that modifies the DataGridRow controls so that they have the expected appearance. So far, this is what I am working with:

    <DataGrid Grid.Row="0" Grid.Column="0" Margin="5,5,5,5" AutoGenerateColumns="False" ItemsSource="{Binding Path=MyData}">
        <DataGrid.Resources>
            <Style x:Key="rowStyle" TargetType="{x:Type DataGridRow}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridRow}">
                            <Border CornerRadius="8,8,8,8" BorderBrush="Red" BorderThickness="2">
                                <ContentPresenter />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Foo"  />
            <DataGridTextColumn Header="Baz" />
            <DataGridTextColumn Header="Bar" />
        </DataGrid.Columns>
   </DataGrid>

该版本显然是基本版本(只是股票模板周围的边框),但是运行该应用程序时看不到任何区别.

This version would obviously be rudimentary (simply a border around the stock template), but I cannot see any difference when I run the application.

那么,问题是,如何为DataGridRow自定义控件模板?或者,如果这不可行,是否有更好的方法可以实现我的目标:?

The question, then, is how do I customize the control template for a DataGridRow? Or, if this is unworkable, is there a better way to go about accomplishing my aims:?

推荐答案

该行的实际模板比这要复杂一些.参见下面的样式-这几乎是基本样式,但是我添加了一些设计,并留下了IsMouseOverIsSelected的触发器(可以将其删除).

The actual template for the row is a bit more complicated than this. See the style below - it's pretty much the basic style but I've added some of your design and left triggers for IsMouseOver and IsSelected (feel free to remove them).

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="Background"
            Value="Transparent" />
    <Setter Property="BorderBrush" 
            Value="Red" />
    <Setter Property="BorderThickness" 
            Value="2" />
    <Setter Property="SnapsToDevicePixels"
            Value="true" />
    <Setter Property="Validation.ErrorTemplate"
            Value="{x:Null}" />
    <Setter Property="ValidationErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <TextBlock Foreground="Red"
                           Margin="2,0,0,0"
                           Text="!"
                           VerticalAlignment="Center" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridRow}">
                <Border x:Name="DGR_Border"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="True"
                        CornerRadius="8,8,8,8">
                    <SelectiveScrollingGrid>
                        <SelectiveScrollingGrid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </SelectiveScrollingGrid.ColumnDefinitions>
                        <SelectiveScrollingGrid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </SelectiveScrollingGrid.RowDefinitions>
                        <DataGridCellsPresenter Grid.Column="1"
                                                ItemsPanel="{TemplateBinding ItemsPanel}"
                                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        <DataGridDetailsPresenter Grid.Column="1"
                                                  Grid.Row="1"
                                                  SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                                                  Visibility="{TemplateBinding DetailsVisibility}" />
                        <DataGridRowHeader Grid.RowSpan="2"
                                           SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
                                           Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                    </SelectiveScrollingGrid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver"
                             Value="True">
                        <Setter TargetName="DGR_Border"
                                Property="Background"
                                Value="LightGray" />
                    </Trigger>
                    <Trigger Property="IsSelected"
                             Value="True">
                        <Setter TargetName="DGR_Border"
                                Property="Background"
                                Value="Gray" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

哦,顺便说一句,您有样式的键,但是您在任何时候都没有引用它-因此该行使用它的默认样式.要使用您的样式或上面提供的样式,请不要提供资源的关键.

Oh, and btw, you have a key for the style but you don't reference it at any point - so the row uses it's default style. To use your style or the one provided above, don't give a key to the resource.

这篇关于在WPF中为DataGridRow创建ControlTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆