Wpf DataGrid Column splitting

2019-08-06 11:59发布

问题:

I want to put a column to specify range in a wpf datagrid. For this, i am thinking to take one column with header "Range" and i want to split that into 2 columns like "Minimum" and "Maximum". Is it possible to represent data like that? If it is, what about the data binding to the datagrid means how binding will change?

回答1:

If you can keep you min/max parts non-resizable, then a fairly simple solution exists:

  1. In your objects create a property of Tuple type or whatever type will represent your min/max structure.
  2. Add a templated column to your grid as follows (sample bindings should work with the Tuple I didn't try it though):

<DataGrid>
  <DataGrid.Columns>
    <DataGridTemplateColumn Header="Min/Max">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <Grid>
            <Grid.RowDefinitions>
              <RowDefinition />
              <RowDefinition />
            </Grid.RowDefinitions>
            <TextBlock Grid.Column="0" Text="{Binding Item1}" />
            <TextBlock Grid.Column="1" Text="{Binding Item2}" />
          </Grid>
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>
</DataGrid>

Bind your grid to the data as normal