c# - 在 WPF 中为 DataGridRow 创建 ControlTemplate

标签 c# wpf xaml controltemplate

我想要完成的是自定义 DataGrid 控件,使每一行都有圆角,没有网格线(只是我正在使用的设计)。

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

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

这个版本显然是初级版本(只是在常用模板周围加了一个边框),但我在运行应用程序时看不出有任何区别。

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

最佳答案

行的实际模板比这复杂一点。请参阅下面的样式 - 它几乎是基本样式,但我添加了一些您的设计并为 IsMouseOverIsSelected 留下了触发器(您可以随意删除它们)。

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

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

关于c# - 在 WPF 中为 DataGridRow 创建 ControlTemplate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14264087/

相关文章:

c# - 从 Nuget.Server 获取 Json 而不是 XML

c# - 如何在初始化时加载用户控件

c# - MVVM - 单击按钮时在哪里调用函数?

c# - 当我切换到不同的 XAML 时应用程序崩溃

c# - 绑定(bind)到通用 UI 元素/XAML 接口(interface)

c# - 如何从模型类生成 Action 链接?

c# - EvaluateScriptAsync挂起

c# - UserControl (*.ascx) 中的自定义属性?

c# - 为什么我的 WPF 翻译动画在完成之前就停止了?

Wpf 应用程序崩溃 - 错误模块名称 coreclr.dll 版本 : 4. 700.20.47201