Hardgraf
Hardgraf

Reputation: 2646

Star sizing Grid columns in an ItemsControl?

I have a ListBox with an ItemsControl bound to a collection in my view model. I'm trying to use star sizing on a grid column within the DataTemplate and set the element within that column (a progress bar) to stretch. This normally would take up all available horizontal space in the grid however nested in an ItemsControl this does not seem to be the case. I've done a bit of reading around & there seems to be known issues using these controls together. Is there a solution to this?

<ListBox>
    <ItemsControl ItemsSource="{Binding WebMappingSourcesCollection}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid 
                    ShowGridLines="True"
                    Grid.IsSharedSizeScope="true"
                    >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <CheckBox 
                        Grid.Column="0"
                        HorizontalAlignment="Center" />
                    <TextBlock 
                        Grid.Column="1" 
                        Text="{Binding Name}"/>
                    <ProgressBar Grid.Column="2"
                        Minimum="0" 
                        Maximum="100" 
                        Value="30"
                        HorizontalContentAlignment="Stretch"                                                           
                        HorizontalAlignment="Stretch"     
                        MaxHeight="15"
                    /> 
                    <!-- etc. etc. -->

Upvotes: 0

Views: 216

Answers (1)

Nitin Purohit
Nitin Purohit

Reputation: 18578

Try setting HorizontalContentAlignment on your listbox item by giving ItemContainerStyle like below.

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Upvotes: 2

Related Questions