tags:

views:

27

answers:

1

Hi, 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???

+1  A: 

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> 
Zamboni
worked like a charm, thx!
sean717