XAML 在WPF中为DataGridRow创建ControlTemplate

qhhrdooz  于 2023-06-19  发布在  其他
关注(0)|答案(2)|浏览(118)

我试图完成的是自定义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定制控件模板?或者,如果这是不可行的,有没有更好的方法来实现我的目标:

57hvy0tb

57hvy0tb1#

该行的实际模板比这个要复杂一些。请看下面的样式--它几乎是基本的样式,但我添加了一些你的设计,并为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>

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

ryevplcw

ryevplcw2#

<Style TargetType="{x:Type DataGridColumnHeader}" x:Key="DatagridColumnHeaderStyle">
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Height" Value="35" />
        <Setter Property="SeparatorBrush" Value="DarkRed" />
        <Setter Property="FontWeight" Value="Black" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                    <Grid>
                        <Border x:Name="columnHeaderBorder"
                                BorderThickness="1"
                                Padding="3,0,3,0">
                           
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Border>
                    </Grid>
                </ControlTemplate>    
            </Setter.Value>
        </Setter>
    </Style>

并且样式可以参考如下

<DataGrid x:Name="myGridView" 
           Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
           Height="200" Margin="5,15,5,0"  
           AutoGenerateColumns="False" 
           ItemsSource="{Binding Person}" 
           SelectedItem="{Binding Path=PersonDetails, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" >
                
    <DataGrid.Columns>

        <DataGridTextColumn HeaderStyle="{StaticResource DatagridColumnHeaderStyle}"  Width="200" Header="Customer Name" Binding="{Binding Path=Name}"/>
        <DataGridTextColumn HeaderStyle="{StaticResource DatagridColumnHeaderStyle}" Width="250" Header="Customer Address" Binding="{Binding Path=Address}"/>
        <DataGridTextColumn HeaderStyle="{StaticResource DatagridColumnHeaderStyle}" Width="100" Header="Order Id" Binding="{Binding Path=OrderId}"/>
               
    </DataGrid.Columns>

相关问题