I'm trying the content of a shopping cart in an ItemsControl(ListBox)
. To do so, I've created the following DataTemplate
:
<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?
DockPanel
s are evil. Temptation to useStackPanel/DockPanel
combination for complex layouts leads to "layout dead ends". Use a Grid:I use
Grid
s almost exclusively, using a separate grid for each block of elements that "belong together"The nice thing about dock panels is they already fill all the available space. LastChildFill is true by default (but I set it below for clarity), so just don't set the DockPanel attribute on the last child, and it will fill the available space.
Bind the
Width
of theDockPanel
to theActualWidth
of theListBoxItem
:Another option: you could just redefine the
ItemContainerStyle
so that theListBoxItem
is stretched horizontally: