0

My WPF application has a ListBox whose ItemTemplate looks like this:

<DataTemplate x:Key="DomainTemplate" DataType="DomainViewModel">
    <Border BorderBrush="{Binding Converter={StaticResource BrushConverter}, Path=IsSelected}"
            BorderThickness="3"
            Grid.Column="0"
            Name="SelectedBorder"
            Padding="5">
        <Button Click="SelectDomain_Click"
                FontSize="16"
                FontWeight="Bold"
                IsEnabled="{Binding Path=CurrentSiteIsValid, RelativeSource={RelativeSource AncestorType={x:Type c:DomainPicker}}}"
                MinHeight="60"
                Tag="{Binding Path=DomainId}"
                Width="120">
            <TextBlock Text="{Binding Path=Name}"
                       TextAlignment="Center"
                       TextWrapping="WrapWithOverflow" />
        </Button>
    </Border>
</DataTemplate>

The window's width is driven by the width of the ListBox. This is by design. There seems to a very large space between the vertical edges of the ListBox and the items in it. Using Snoop, I see that the ListBoxItem contains a Border that is the same width as the ListBox, has a Margin of 0, and Padding set to 2,0,0,0.

The Border contains a ContentPresenter whose width is 29 units smaller than the Border that contains it. The Padding on the Border would seem to account for 2 of the units. Its Margin is 0 and it has no padding property.

I actually would like to make this window a bit narrower if I can without making the Buttons in the template any narrower. Where is that 29 unit space coming from? Is there any way to change its size?

Tony Vitabile
  • 8,298
  • 15
  • 67
  • 123
  • If I understand correctly, you may have to set the ItemContainerStyle as show [here](http://stackoverflow.com/a/2924249/187697). – keyboardP Aug 23 '13 at 21:55

1 Answers1

0

I've ran into this problem too in which I used a WrapPanel as the ListBox's ItemPanel, and i ended up with a gap between items in certain cases due to the problem you mentioned. Here is the fix:

<ListBox.ItemContainerStyle>
   <Style TargetType="ListBoxItem">
      <Setter Property="Padding" Value="0"/>
   </Style>
</ListBox.ItemContainerStyle>
Maximus
  • 1,441
  • 14
  • 38