WPF - Why Listbox items do not fill uniformgrid
Asked Answered
G

2

4

I have a listbox with the ItemsPanelTemplate set to UniformGrid with rows= 6 and cols= 7.

I want the listbox items to fill their space.

I am using a datatemplete defined in a dictionary.

The outer control of my template is a Border with HorizontalAlignment=Stretch and VerticalAlignent=Strectch but the templates do not fill the listbox items space?

Any ideas? Malcolm

Gaitskell answered 30/12, 2008 at 7:21 Comment(2)
Can you provide a sample XAML?Bihar
possible duplicate of Items do not fill wpf listbox using datatemplateMagus
G
11

The answer to this to set HorizontalContentAligment and VerticalContentAlignment to Stretch on the LISTBOX not the datatemplate.

Gaitskell answered 3/1, 2009 at 12:1 Comment(0)
T
2

EDITED: Added additional information, and replied to question.

An interesting way to make ListBoxItems be uniform with other items is to the Grids shared scope feature in your DataTemplate

Example:

<ItemsControl Grid.IsSharedSizeScope="True" ItemsSource="{Binding Path=Items}">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="Content"/>
          </Grid.ColumnDefinitions>
          <TextBlock Grid.Column="1" Text="{Binding Path=Name}">
        </Grid>
      </StackPanel>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

Now all the TextBlocks will be the same size in your layout. The child item should fill all available space if no specific width/height are set.

Alternatively you can set the Width and Height of the control to stretched, however I think using the Grid.SharedScopeSize is a more elegant way to achieving the same effect.

Teeterboard answered 30/12, 2008 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.