tags:

views:

1069

answers:

2

I'm trying the content of a shopping cart in an ItemsControl (ListBox). To do so, I've created the following data template:

<DataTemplate x:Key="Templates.ShoppingCartProduct"
              DataType="{x:Type viewModel:ProductViewModel}">
    <DockPanel HorizontalAlignment="Stretch">
        <TextBlock DockPanel.Dock="Left"
                   Text="{Binding Path=Name}"
                   FontSize="10"
                   Foreground="Black" />
        <TextBlock DockPanel.Dock="Right"
                   Text="{Binding Path=Price, StringFormat=\{0:C\}}"
                   FontSize="10"
                   Foreground="Black" />
    </DockPanel>
</DataTemplate>

When the items are displayed in my shopping cart however, the Name and Price TextBlocks are sitting right beside one another, and there is an extremely large amount of whitespace on the right hand side.

Was wondering what the best method to force the DockPanel to stretch to fill all the space made available by the ListItem was?

+1  A: 

Bind the Width of the DockPanel to the ActualWidth of the ListBoxItem :

<DockPanel Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
...

EDIT: another option : you could just redefine the ItemContainerStyle so that the ListBoxItem is stretched horizontally :

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
    </Style>
</ListBox.ItemContainerStyle>
Thomas Levesque
I tried using that binding and it seems to be causing the ListBoxItem to continually grow in size, when viewing with Snoop, I saw the width of both the ListBoxItem and the DockPanel exceed 300,000.
Richard C. McGuire
Try binding to the ActualWidth of the ListBox itself, then...
Thomas Levesque
Oh, ok, I get it... you must set LastChildFill="False" on the DockPanel, otherwise the second TextBlock is stretched
Thomas Levesque
I ended up locating your second approach and going that route.
Richard C. McGuire
A: 

DockPanels are evil. Temptation to use StackPanel/DockPanel combination for complex layouts leads to "layout dead ends". Use a Grid:

<Grid>
  <TextBlock HorizontalAlignment="Left"
...
  <TextBlock HorizontalAlignment="Right"
...
/Grid>

I use Grids almost exclusively, using a seperate grid for each block of elements that "belong together"

Sergey Aldoukhov
I don't think DockPanels are evil, they can be pretty useful sometimes... however I must agree that it was probably not the best option in that case
Thomas Levesque
Of course this is subjective and they are not an _absolute_ evil ;) But look where Rich already going - using ItemContainerStyle (semi-advanced stuff) for simple task - kinda indicative...
Sergey Aldoukhov
I had considered a Grid at the outset. However, given a narrow ListBox, or long enough value for the Name or Price properties the two TextBlocks will end up overlapping their values. In addition I would hardly classify laying out two TextBlocks at opposite ends of a panel as a "complex layout".
Richard C. McGuire