问题
I am making UI as shown in picture below.
It is a DataGrid, Itemsource is bound to a List. I cannot set ColumnSpan
for the TextBox
. First, I tried it with a UserControl
but I couldn't fix DataGridTemplateColumn Header, then I tried DataGridTemplateColumn.CellTemplate but couldn't set the ColumnSpan
.
Is there any way/method to make this kind of Datagrid?
Thank you in advance.
回答1:
There is no ColumnSpan
in the DataGrid
, like in the normal Grid
.
To get this kind of layout you have 3 choices, all with heavy drawbacks unfortunately.
1 merge cells
Use the merge event to merge cells together in code behind. See a guide to this here This requires a large chunk of code behind coding to get this right with your layout. So I would really advise against this.
2 RowDetails
Use the build in RowDetails
. This doesn't get the layout exactly like your's but is the easiest and closest build in function to your requirement.
It will look something like this:
It's easy to configure:
Set: RowDetailsVisibilityMode="Visible"
in the DataGrid XAML.
And define your template for the row:
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<TextBlock Text="{Binding Desc}" Background="LightGray"/>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
[+]
All DataGrid functions will work: Sorting, Adding Rows, Resizing ...
[-]
The Details section span over the whole row.
See this tutorial about what you can do with row details.
3 Hack something together
With templates and code behindz. Some resources that might help you:
- Override Row Template
- Build a custom cell template
- DataGrid in general
回答2:
I have come with other solution using Grids, List and UserControl. In a simple way, I made it as following:
来源:https://stackoverflow.com/questions/39071767/datagridtemplatecolumn-column-span-in-datagrid-wpf