I have a ListBox
containing a DataTemplate, where I want to use a Dockpanel to let the last Child fill up the available space (Last Child in my case is the Textblock, which will be rendered in the center).
To do this, I have the following code (with additional DataTemplate on the ComboBox removed, since it isn't needed in the context of my question):
<ListBox x:Name="IsoLimitsList" ItemsSource="{Binding ISOLimits}" SelectedValue="{Binding SelectedISOLimit}" Margin="10, 0" BorderThickness="0"
Padding="0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel LastChildFill="True" Width="{Binding Path=ActualWidth, ElementName=IsoLimitsList}">
<CheckBox IsChecked="{Binding IsActive}" DockPanel.Dock="Left" VerticalAlignment="Center"/>
<ComboBox ItemsSource="{Binding Path=DataContext.AllOxyColors, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
Width="150" DockPanel.Dock="Right" SelectedValue="{Binding Color, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Value" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
<TextBlock Text="{Binding Name}" Margin="5, 2" VerticalAlignment="Center" HorizontalAlignment="Stretch" />
</DockPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This works perfectly fine, except that the Dockpanel is just a few pixels too big, resulting in a Scrollbar within the Listbox:
Question is: Where does the extra width come from? Can I remove the extra width without having to write another dataconverter?
What I have tried so far:
- Removed changed Padding/Margin values of all the elements, but they all work as expected.
- Give the ListBox a fixed Width instead letting it stretch within its parent. Doesn't solve the issue.