Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

control in ListView.GridViewColumn alignment

Tags:

wpf

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

like image 672
sean717 Avatar asked Oct 01 '10 18:10

sean717


1 Answers

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> 
like image 96
Zamboni Avatar answered Oct 18 '22 08:10

Zamboni