views:

126

answers:

2

So I have this Xaml inside a ListBox Item Template:

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid Height="22" Width="Auto">
            <Image Margin="-2,0,0,0" Source="{Binding Path=ADsPath, Converter={StaticResource ImxConverter}}" HorizontalAlignment="Left" Width="22"  />
            <TextBlock Margin="20,3,0,0" Text="{Binding Path=DisplayValue}" Width="Auto" />
            <Rectangle Fill="White" Stroke="White" Margin="-2,0,-2,0.5" VerticalAlignment="Bottom" Height="1" Width="Auto" />
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

The idea is that the rectangle, provides a thin white line across the bottom of the entire ListBox Item; however, with the Xaml above, it only extends as long as the text, not to the full width of the ListBox.

+1  A: 

Setting your width to Auto basically tells it to only be large enough to fit everything inside. I think you need to set your Grid's HorizontalAlignment to Stretch for it to work properly.

Edit:

I did a small sample app. Here's how I would do what you are trying to do:

On your actual listbox, I would have the HorizontalContentAlignment property set to Stretch

and

I would change your Grid to a DockPanel:

<ListBox.ItemTemplate>
    <DataTemplate>
        <DockPanel Height="22" HorizontalAlignment="Stretch">
            <Rectangle Fill="White" Stroke="White" Margin="-2,0,-2,0.5" DockPanel.Dock="Bottom" Height="1"/>
            <Image Margin="-2,0,0,0" Height="20" DockPanel.Dock="Left" Width="22"  />
            <TextBlock Margin="20,3,0,0" Text="Daniel Manning" DockPanel.Dock="Left"/>
        </DockPanel>
    </DataTemplate>
</ListBox.ItemTemplate>
Scott
Should I remove the Width="Auto" from all child elements as well or only the Grid?
Nate Bross
I don't think you'll need to, I think your TextBlock's Width is Auto by default (not sure), you may want to remove it from your rectangle, and change the rectangle's HorizontalAlignment to stretch though.
Scott
See Edit: Only reason I suggest a DockPanel over Grid is because a Grid is a lot more expensive, and a Dock Panel seems much more efficient for what you're trying to accomplish.
Scott
:) For text with a Bears player name! I ended up setting HorizontalContentAlignment="Stretch" on the listbox but am still using the Grid, I'll check into the DockPanel, but typically I'm not going to have more that 10/20 items on the list.
Nate Bross
+1  A: 

Have you tried removing Width="Auto"? Auto is saying "only make me as big as I need to be" which, in your case, is determined by the length of the text. The default is "Stretch" which means "hey container, do me a favor and make me as wide as you are".

Drew Marsh