简体   繁体   English

WPF DataGrid中的Colspan

[英]Colspan in WPF DataGrid

I have a datagrid in one of my WPF application where I am showing some data from an XML file. 我的WPF应用程序之一中有一个数据网格,其中显示了XML文件中的一些数据。 Now there are rows which are kind of a headers. 现在有一些行是标题。 I want to show those rows with colspan, so that it occupies the whole row. 我想用colspan显示那些行,以便它占据整个行。 I have tried the below code but it is not working. 我已经尝试了以下代码,但无法正常工作。

<DataGrid.RowStyle>
  <Style TargetType="DataGridRow">    
      <Style.Triggers>
         <DataTrigger Binding="{Binding Type}" Value="BorderCheck">
              <Setter Property="Grid.ColumnSpan" Value="6" />
         </DataTrigger>                        
      </Style.Triggers>
  </Style>
</DataGrid.RowStyle>

There are 2 things if you need column span to all the rows there's no point adding extra columns if you need column span to particular rows you need set it at the row level which should be the element you want to expand to other columns like below 有两件事,如果您需要所有行的列跨度,那么没有必要添加额外的列,如果您需要特定行的列跨度,则需要在行级别将其设置为应该扩展到其他列的元素,如下所示

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="100"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="5*" />
            <ColumnDefinition Width="5*" />
        </Grid.ColumnDefinitions>
        <Label  Grid.Row="0" Grid.Column="0" FontSize="20" >Name</Label>
        <TextBox Grid.Row="1" Grid.ColumnSpan="2" BorderThickness="5" />
    </Grid>

Ouput会像这样

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

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