Using a Grid as an ItemsHost

孤街醉人 提交于 2019-12-04 16:22:55
Kent Boogaart

Because you're using an ItemsControl, a container is generated for each item. That container (an instance of ContentPresenter for a plain old ItemsControl) wraps the TextBlock, and is a direct child of the Grid. Therefore, the Grid never even sees the Column and Row properties on the TextBlock because it's looking instead at the container.

You can solve this by setting an ItemContainerStyle that binds the appropriate properties for the container:

<ItemsControl.ItemContainerStyle>
    <Style TargetType="ContentPresenter">
        <Setter Property="Grid.Column" Value="{Binding Column}"/>
        <Setter Property="Grid.Row" Value="{Binding Row}"/>
    </Style>
</ItemsControl.ItemContainerStyle>

Possible workaround: if you use UniformGrid, you may not need to specify the rows and columns.

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