WPF GridViewRowPresenter in an ItemsControl

依然范特西╮ 提交于 2019-12-05 09:56:21
Kent Boogaart

If you want a non-interactive grid of items, you can use an ItemsControl with a Grid that uses shared size scope:

<ItemsControl ItemsSource="{Binding Items}" Grid.IsSharedSizeScope="True">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" SharedSizeGroup="FirstName"/>
                    <ColumnDefinition Width="*" SharedSizeGroup="LastName"/>
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding FirstName}"/>
                <TextBlock Grid.Column="1" Text="{Binding LastName}"/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

A more efficient approach would be to write your own Panel subclass that works similarly to Grid (you could probably subclass Grid) but automatically adds rows as necessary. Then use that Panel as the ItemsPanel for the ItemsControl.

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