I have following WPF ListView:
<ListView Grid.Column="2" Grid.Row="1" Margin="0,53,12,6"
Name="lvwProperties"
ItemsSource="{Binding Path=SelectedPropertyItems, Mode=TwoWay}"
Grid.ColumnSpan="2">
<ListView.View >
<GridView>
<GridViewColumn Header="Property" DisplayMemberBinding="{Binding Path=PropertyName, Mode=TwoWay}" Width="130" />
<GridViewColumn Header="Is Mandatory" Width="90" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsMandatory, Mode=TwoWay}" HorizontalAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I want to make the CheckBox aligned center in the 2nd GridColumn. Set the HorizontalAlignment="Center"
seems doesn't do the trick. How to achive this in XAML???
To center content in a ListView you need to set ListView.ItemContainerStyle to have a horizontal alignment of Stretch as follows:
<ListView Grid.Column="2" Grid.Row="1" Margin="0,53,12,6" Name="lvwProperties"
ItemsSource="{Binding Path=SelectedPropertyItems, Mode=TwoWay}"
Grid.ColumnSpan="2">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Property" DisplayMemberBinding="{Binding Path=PropertyName, Mode=TwoWay}" Width="130" />
<GridViewColumn Header="Is Mandatory" Width="90" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=IsMandatory, Mode=TwoWay}" HorizontalAlignment="Center" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With