1
votes

Basically I'm getting some data from a service and displaying the results in a listbox. The template for the items is using a grid. NOTE: If there is a better way let me know.

<Grid>
   <Grid.ColumnDefinitions>
       <ColumnDefinition/>
       <ColumnDefinition/>
   </Grid.ColumnDefinitions>
   <Image Grid.Column="0"/>
   <TextBlock Grid.Column="1"/>
</Grid>

The problem is, that sometimes an image is not returned. In that case the column for the image should collapse and the text column should take up the full width.

enter image description here

I've tried a couple of different ways already with no luck. How can I collapse this column when no image is returned?

2

2 Answers

2
votes
<Grid>
   <Grid.ColumnDefinitions>
       <ColumnDefinition Width="Auto"/>
       <ColumnDefinition Width="*"/>
   </Grid.ColumnDefinitions>
   <Image Grid.Column="0"/>
   <TextBlock Grid.Column="1"/>
</Grid>

Setting the image column width to auto will resize the column width according to the image size. If there is no image the size will be set to 0. The text column is set to *, this way it always takes all the available space.

Note: If your images are big, you may need to set MaxWidth as well.

0
votes

You can use Expander control and set IsExpanded property.