如何在WPF中仅在DataGrid中创建行下带圆角的实心背景

ddrv8njm  于 2023-03-04  发布在  其他
关注(0)|答案(2)|浏览(247)

我想使数据网格与坚实的背景下圆角行只。标题行必须没有背景。
我尝试了几个选项来通过(行/标题/数据网格)样式实现这一点,但都不成功。
我想最好的方法是使用绑定到DataGrid行顺序的DataTrigger(第一行和最后一行使用一个模板,中间行使用第二个模板),但是我找不到行顺序属性。我希望所有的帮助。
一定是这样的。

z18hc3ub

z18hc3ub1#

你必须覆盖datagrid的模板(听起来比实际要复杂得多):

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGrid}">
                <!-- extra attributes as example, but you can tweak whatever you want in the template, what is important is the "CornerRadius" here -->
                <Border Name="Border"
                        Padding="{TemplateBinding Padding}"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="1"
                        CornerRadius="3">
                    <ScrollViewer Name="DG_ScrollViewer" Focusable="false" CanContentScroll="True" IsDeferredScrollingEnabled="true">
                        <!-- here you can also overwrite the Scrollviewer's Template if you wish -->
                        <ItemsPresenter SnapsToDevicePixels="True" />
                    </ScrollViewer>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这个样式应该放在/theme/Generic.xaml或者你的app.xaml中(或者你想要的任何xaml字典中,只要它被正确引用)

nmpmafwu

nmpmafwu2#

我已经找到了解决方案。我已经采取了默认的数据网格模板,并把ItemsPresenter到边界与所需的参数。然后我把这个模板应用到数据网格。

<ControlTemplate x:Key="dg_solid_without_header" TargetType="{x:Type DataGrid}">
            <Border x:Name="border"
                SnapsToDevicePixels="True"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Padding="{TemplateBinding Padding}">
                <Border.Background>
                    <SolidColorBrush Color="{DynamicResource ControlLightColor}" />
                </Border.Background>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Disabled">
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames Storyboard.TargetName="border"
                                                Storyboard.TargetProperty="(Panel.Background).
                      (SolidColorBrush.Color)">
                                    <EasingColorKeyFrame KeyTime="0"
                                         Value="{DynamicResource ControlLightColor}" />
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </VisualState>
                        <VisualState x:Name="Normal" />
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <ScrollViewer x:Name="DG_ScrollViewer"
                        Focusable="false"
                        Background="Black">
                    <ScrollViewer.Template>
                        <ControlTemplate TargetType="{x:Type ScrollViewer}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="Auto" />
                                </Grid.RowDefinitions>

                                <DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter"
                                                  Grid.Column="1"
                                                  Visibility="{Binding HeadersVisibility, 
                      ConverterParameter={x:Static DataGridHeadersVisibility.Column}, 
                      Converter={x:Static DataGrid.HeadersVisibilityConverter}, 
                      RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />

                                <ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
                                          Grid.ColumnSpan="2"
                                          Grid.Row="1"
                                          CanContentScroll="{TemplateBinding CanContentScroll}" />

                                <ScrollBar x:Name="PART_VerticalScrollBar"
                             Grid.Column="2"
                             Grid.Row="1"
                             Orientation="Vertical"
                             ViewportSize="{TemplateBinding ViewportHeight}"
                             Maximum="{TemplateBinding ScrollableHeight}"
                             Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                             Value="{Binding VerticalOffset, Mode=OneWay, 
                      RelativeSource={RelativeSource TemplatedParent}}"/>

                                <Grid Grid.Column="1"
                        Grid.Row="2">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="{Binding NonFrozenColumnsViewportHorizontalOffset, 
                          RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>

                                    <ScrollBar x:Name="PART_HorizontalScrollBar"
                               Grid.Column="1"
                               Orientation="Horizontal"
                               ViewportSize="{TemplateBinding ViewportWidth}"
                               Maximum="{TemplateBinding ScrollableWidth}"
                               Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"
                               Value="{Binding HorizontalOffset, Mode=OneWay, 
                        RelativeSource={RelativeSource TemplatedParent}}"/>
                                </Grid>
                            </Grid>
                        </ControlTemplate>
                    </ScrollViewer.Template>
                    <Border Background="#0F1F2F" BorderBrush="#114B6F" BorderThickness="2" CornerRadius="8">
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    </Border>
                </ScrollViewer>
            </Border>
        </ControlTemplate>


<DataGrid Name="dg_upper_bracket_results_table_cla" Margin="243 20 243 0" Style="{StaticResource new_CLA_table_style}" Template="{StaticResource dg_solid_without_header}" ItemsSource="{Binding UpperBracketTable}">
                            <DataGrid.Columns>
                                <StaticResource ResourceKey="dgtcPosition_new_cla"/>
                                <StaticResource ResourceKey="dgtcPlayerWithDivision_new_cla"/>
                                <StaticResource ResourceKey="dgtcTeam_new_cla"/>
                                <StaticResource ResourceKey="dgtcWins"/>
                                <StaticResource ResourceKey="dgtcDraws"/>
                                <StaticResource ResourceKey="dgtcLosses"/>
                                <StaticResource ResourceKey="dgtcGoals"/>
                                <StaticResource ResourceKey="dgtcPoints"/>
                            </DataGrid.Columns>
                            <DataGrid.RowStyle>
                                <Style TargetType="DataGridRow">
                                    <Setter Property="Template" Value="{StaticResource default_datagridrow_style}"/>
                                </Style>
                            </DataGrid.RowStyle>
                        </DataGrid>

相关问题