0

I would like to stretch a listbox similar to that :

<Grid Background="#FFC0BBBB">
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Border HorizontalAlignment="Stretch" Grid.Row="0">
            <ListBox  Name="listBox"   >
                <ListBoxItem >
                    <StackPanel>
                        <TextBlock Text="Toto"></TextBlock>
                        <!--<Image Source ="" Visibility="Collapsed" />-->
                    </StackPanel>
                </ListBoxItem>
                <ListBoxItem >listbox item 2</ListBoxItem>
                <ListBoxItem >
                    <Grid>
                    <TextBlock Text="Tata"></TextBlock>
                    <Image Source ="" />
                    </Grid>
                </ListBoxItem>
                <ListBoxItem >
                    <Grid>
                        <TextBlock Background="Aqua" Text=""></TextBlock>
                        <Image Source ="https://support.twitter.com/images/twitter-bird.png?1339467554" />
                    </Grid>
                </ListBoxItem>
                <ListBoxItem >
                    <Grid>
                        <TextBlock Text="Vava"></TextBlock>
                        <Image Source ="" />
                    </Grid>
                </ListBoxItem>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="5" Background="Pink" HorizontalAlignment="Stretch" />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>               
             </ListBox>
        </Border>
</Grid>

If you put just content like for the second item it works perfectly, but my "real" ListBox will be bind and for some items I need to put an Image for others a TextBlock (I neved need Image+Textblock). I think that I will have to use a converter but I don't know "where" to put it and what should he returns.

Any ideas ? Thanks

Pak
  • 2,639
  • 2
  • 21
  • 27

1 Answers1

0

If you are binding to data, and then using properties of your data to work out what kind of control you'll need in your ListBoxItem, then you need to use a DataTemplateSelector

http://msdn.microsoft.com/en-us/library/system.windows.controls.datatemplateselector.aspx

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90