tags:

views:

32

answers:

2

I want to create list of buttons, and i want buttons to spread across the list item space. Here is what i have:

    <ListBox Margin="44,54,134,0" Name="listView1" Height="64" >
        <Button Height="20"></Button>
        <Button Height="20"></Button>            
    </ListBox>

Result is:

alt text

First pic

I want something like second picture, but right side of button to stick to right side of list. I tried to bind in ItemTemplate to ListBox width, but this doesn't work for all cases (if width is Auto)

Thanks, Andrey

+1  A: 
<ListBox Margin="44,54,134,0" Name="listView1" Height="64" HorizontalContentAlignment="Stretch">
    <Button Height="20"></Button>
    <Button Height="20"></Button>
</ListBox>

Edit from Andrey

If you want this solution to be perfect add these lines:

        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Padding" Value="0"></Setter>
            </Style>
        </ListBox.ItemContainerStyle>

they will remove nasty gap at the left, that make list assymetrical

Dan Bryant
wow, so f***ing simple!!!! i found more complex solution, check my answer if you are interested.
Andrey
You can also apply a margin to the button controls if you like, which can adjust the gap to the left and right; you can use negative margins to compensate for the Padding in the ListBoxItem container without restyling the container. The disadvantage is that you have to apply the margin to each button.
Dan Bryant
A: 

here is how i managed to solve this. this is more flexible, but it is really overhead for such a simple task

    <ListBox ItemsSource="{Binding}">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid>
                                <Button Height="20">
                                    <ContentPresenter></ContentPresenter>
                                </Button>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"></TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
Andrey