WPF - Why Listbox items do not fill uniformgrid

不打扰是莪最后的温柔 提交于 2019-12-04 10:01:11

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

Dennis

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!