0

Given a WPF DataGrid with nine columns, which is being populated programmatically through a binding, and has nine columns:

| 1 | 2 | a | 3 | 4 | b | 5 | 6 | c |

What I'd like to do is draw the headers as shown, keeping their width, but not actually display the column header text if the column header is a character. So I'd like it to look like this:

| 1 | 2 |   | 3 | 4 |   | 5 | 6 |   |

I'm not sure of how to achieve this. Perhaps I can override the actual rendering method that is called when the column is constructed?

Tony
  • 3,587
  • 8
  • 44
  • 77
  • https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.datagridcolumn.headerstyle?view=windowsdesktop-5.0 might give you some context, but in terms of the spacing between the columns `2` & `3` I'm not too sure, why don't you just separate them into 3 grids with 2 columns each instead? – ConnorTJ Oct 29 '21 at 09:24
  • I could place them into separate grids, but I need to be able to manage user selection, and tracking which cells have been selected across multiple grids could be a bit of a headache – Tony Oct 29 '21 at 09:26
  • I believe you could create a margin that you add on the columns for `2` and `3` and remove the need for the `a`, `b`, `c` columns? It would create the visual gap between the columns, but keep the control unified and allow you to manage the cell selection easier – ConnorTJ Oct 29 '21 at 09:32
  • That was something I considered. Although I'm not sure how I would implement that margin, and keep the text aligned in the correct place. Is it possible to show an example of this? Thanks – Tony Oct 29 '21 at 09:33
  • You could always use a converter to check whether the binded value is a letter - in which case you return "". Keeping the spacing is just a matter of giving the column a fixed size (which could be "1*" so every column has the same size) – Shrimperator Oct 29 '21 at 09:39
  • I did also consider using a converter, but as the columns are added programmatically, or should I say dynamically, I'm not sure how I would set that converter in code. As none of the columns are defined in the XAML. Thanks – Tony Oct 29 '21 at 09:42
  • @Tony I believe this might be what you're looking for https://stackoverflow.com/a/4460888/13108684 – ConnorTJ Oct 29 '21 at 09:48
  • That looks like it might be very useful, thanks. Only issue is that the columns are being defined in the XAML, and mine are not. So I'd need to figure out how to do this in code. Thanks though – Tony Oct 29 '21 at 09:56
  • @Tony this may help with specifying the Columns style within code https://stackoverflow.com/a/17912019/13108684 – ConnorTJ Oct 29 '21 at 09:58
  • Great, thanks. I'll do some reading, appreciate the help. – Tony Oct 29 '21 at 10:00

0 Answers0