简体   繁体   English

C#WPF-数据网格中的备用空白行

[英]C# WPF - Alternate blank row in datagrid

I will like to add alternate blank row on my datagrid when data fetch from database. 从数据库中获取数据时,我想在数据网格上添加备用空白行。 (Although AlternationCount="2" AlternatingRowBackground="LightGray" was used and it do help to visually segregate row to row, it will better if the row to row data, it will having blank row to have greater visual experience) (尽管使用了AlternationCount="2" AlternatingRowBackground="LightGray" ,它确实有助于在视觉上分隔行与行,但如果行与行之间的数据会更好,它将具有空白行以具有更好的视觉体验)

Thanks 谢谢

I is not a good idea to change visual expirience by adding some waste data. 通过添加一些废数据来更改视觉体验不是一个好主意。 Probably you just need to modify style of your datagrid rows adding some extra space which you may fill as you wish. 可能您只需要修改数据网格行的样式即可添加一些额外的空间,您可以根据需要填充这些空间。

For example, add new style to your resources: 例如,向您的资源添加新样式:

<Style x:Key="MyDataGridRowStyle" TargetType="{x:Type DataGridRow}">
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <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">
                    <SelectiveScrollingGrid>
                        <SelectiveScrollingGrid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </SelectiveScrollingGrid.ColumnDefinitions>
                        <SelectiveScrollingGrid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Name="ExtraRow" Height="20"/>
                        </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}}}"/>
                        <TextBlock Grid.Row="2" Grid.Column="1" Text=" *** Extra space *** " FontSize="8" VerticalAlignment="Center" Margin="100,0"/>
                    </SelectiveScrollingGrid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsNewItem" Value="True">
            <Setter Property="Margin" Value="{Binding NewItemMargin, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
        </Trigger>
    </Style.Triggers>
</Style>

Then use it in your DataGrid: 然后在您的DataGrid中使用它:

<DataGrid RowStyle="{StaticResource MyDataGridRowStyle}">

You will get something like: 您将得到类似:

在此处输入图片说明

ADDED: And template 添加:和模板

<ControlTemplate TargetType="{x:Type DataGridRow}">
    <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
        <SelectiveScrollingGrid>
            <SelectiveScrollingGrid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </SelectiveScrollingGrid.ColumnDefinitions>
            <SelectiveScrollingGrid.RowDefinitions>
                <RowDefinition Name="ExtraRow1" Height="10"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Name="ExtraRow2" Height="10"/>
            </SelectiveScrollingGrid.RowDefinitions>
            <Border Grid.Row="0" Grid.Column="1" BorderThickness="0,0,0,1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderBrush="Black"/>
            <DataGridCellsPresenter Grid.Column="1" Grid.Row="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            <DataGridDetailsPresenter Grid.Column="1" Grid.Row="2" 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="4" 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>

looks like 看起来像

在此处输入图片说明

If you are binding a collection with your datagrid then it will not be possible because datagrid is filled by looking at the source of its items. 如果您将集合与您的datagrid绑定,则将不可能,因为datagrid是通过查看其项目的来源来填充的。 I have a solution in mind but I don't think it will be appreciated. 我有一个解决方案,但我不认为会受到赞赏。

In your collection enter an empty object at each alternating index. 在您的集合中,在每个交替索引处输入一个空对象。 It will create an empty row in datagrid. 它将在datagrid中创建一个空行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM