How to fill/centre WPF ListView columns
I created a ListView with a column of checkboxes that I wanted centred as below:
Sounds like it should be easy, but it's a bit more tricky than just setting the CheckBox.HorizontalAlignment="Center"
in your CellDataTemplate
(which is what I tried first) as you still end up with it left-aligned if you do that.
I stumbled upon this post by Ian Griffiths that explains how to do it. In a nutshell, the problem occurs because the CheckBox
control above is actually the child of a ListViewItem
container, whose default alignment is Left
(which does seem a bit nuts). So, as Ian points out, if you override the ListView.ItemContainerStyle
and set its HorizontalContentAlignment
to Stretch
you can then Center
or Stretch
your child controls correctly.