在数据网格中设置行的样式 [英] Style rows in a datagrid

查看:64
本文介绍了在数据网格中设置行的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在绑定到项源的数据网格中设置行的样式?

How do you style a row in a datagrid bound to an itemssource?

推荐答案

您可以使用as
RowStyle="{DynamicResource MyDataGridRow}"

可以像这样实现:

<Style x:Key="ClientSearchResultsViewDataGridRow" TargetType="{x:Type Custom:DataGridRow}">
  <Setter Property="Background" Value="{x:Null}"/>
  <Setter Property="SnapsToDevicePixels" Value="True"/>
  <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
  <Setter Property="ValidationErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <TextBlock
          Margin="2,0,0,0"
          VerticalAlignment="Center"
          Foreground="Red"
          Text="!">
          <Run Text="!"/>
        </TextBlock>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Custom:DataGridRow}">
        <Border
          x:Name="DGR_Border"
          Background="#FF141F29"
          BorderBrush="{TemplateBinding BorderBrush}"
          BorderThickness="{TemplateBinding BorderThickness}"
          SnapsToDevicePixels="True">
          <Custom:SelectiveScrollingGrid>
            <Custom:SelectiveScrollingGrid.ColumnDefinitions>
              <ColumnDefinition Width="0"/>
              <ColumnDefinition Width="*"/>
            </Custom:SelectiveScrollingGrid.ColumnDefinitions>
            <Custom:SelectiveScrollingGrid.RowDefinitions>
              <RowDefinition Height="*"/>
              <RowDefinition Height="Auto"/>
            </Custom:SelectiveScrollingGrid.RowDefinitions>
            <Custom:DataGridCellsPresenter
              Grid.Column="1"
              Background="#FF141F29"
              ItemsPanel="{TemplateBinding ItemsPanel}"
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
            <Custom:DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" Visibility="{TemplateBinding DetailsVisibility}">
              <Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation>
                <Binding Path="AreRowDetailsFrozen" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Custom:DataGrid}}">
                  <Binding.ConverterParameter>
                    <Custom:SelectiveScrollingOrientation>Vertical
                    </Custom:SelectiveScrollingOrientation>
                  </Binding.ConverterParameter>
                </Binding>
              </Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation>
            </Custom:DataGridDetailsPresenter>
            <Custom:DataGridRowHeader Grid.RowSpan="2" Custom:SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical">
              <Custom:DataGridRowHeader.Visibility>
                <Binding Path="HeadersVisibility" RelativeSource="{RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type Custom:DataGrid}}">
                  <Binding.ConverterParameter>
                    <Custom:DataGridHeadersVisibility>Row
                    </Custom:DataGridHeadersVisibility>
                  </Binding.ConverterParameter>
                </Binding>
              </Custom:DataGridRowHeader.Visibility>
            </Custom:DataGridRowHeader>
          </Custom:SelectiveScrollingGrid>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

就是这样-重新样式化的DataRow.

And that''s it - a restyled DataRow.


行?什么意思您可以自定义列的样式.这很明显,但是行,尤其是将数据绑定到ItemSource的行……这意味着一行将是一种样式,而下一行将是另一种样式,那是怎么回事?什么使不同行的样式不同,因为行的内容由数据定义,并且纯数据不能具有样式"?列是不同的,因为面部中的列表示元数据或数据.

我只能建议您使用分组.这样,行可以按组另外进行分隔,并且可以为每个组分配不同的组样式.

有关示例,请查看本教程: http://www.wpftutorial.net/DataGrid.html [ ^ ].查看分组"部分及其XAML示例,请注意DataGrid.GroupStyle.

—SA
Rows? What would it mean? You can customize the styles for columns; this is clear, but rows, especially with data binding to ItemSource… It means that one line will be one style and next line in another, how''s that? What should make style of the different rows different, as the content of the row is defined by the data, and the pure data cannot have "style"? Columns are different, because a column in face represents meta-data or the data.

I can only suggest you use grouping. In this way, the rows could be additionally segregated by groups, and you could attribute a different group style to each group.

For a sample, look at this tutorial: http://www.wpftutorial.net/DataGrid.html[^]. Look at the section "Grouping" and its XAML sample, pay attention at DataGrid.GroupStyle.

—SA


这篇关于在数据网格中设置行的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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