WPF Grid为ItemsPanel,用于动态绑定到ItemsControl的列表 [英] WPF Grid as ItemsPanel for a list dynamically bound to an ItemsControl

查看:1413
本文介绍了WPF Grid为ItemsPanel,用于动态绑定到ItemsControl的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Grid作为ItemsPanel用于动态绑定到ItemsControl的列表。下面的代码正在运行 - 还有一个问题:我找不到动态初始化GridDefinitions和RowDefinitions的方法。因此,所有的值都放在彼此之间。

I am using a Grid as ItemsPanel for a list dynamically bound to an ItemsControl. The code below is working - with a remaining problem: I can’t find a way to dynamically initialize the ColumnDefinitions and RowDefinitions of the grid. As consequence all values are placed on top of each other.

<ItemsControl ItemsSource="{Binding Cells}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style>
            <Setter Property="Grid.Row" Value="{Binding RowIndex}"/>
            <Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

请注意,我正在根据MVVM模式搜索答案。因此,子类和代码背后只是解决方法,但没有解决方案。

Please be aware, that I am searching an answer according the MVVM pattern. Therefore sub classing and code behind are just workarounds, but no solutions.

推荐答案

你需要一些方法来告诉网格它有许多行/列。也许随着每个项目加载,您可以检查 RowIndex ColumnIndex 的值,如果需要,可以将Rows / Columns添加到Grid

You'll need some way to tell the Grid how many Rows/Columns it has. Perhaps as each Item loads you could check the value of RowIndex and ColumnIndex and add Rows/Columns to the Grid if needed.

作为另一个选择,也许您可​​以公开 RowCount ColumnCount 属性返回最大 RowIndex ColumnIndex ,并且在Grid的Loaded事件中添加了许多您需要的列/行。

As another alternative, perhaps you can expose RowCount and ColumnCount properties in your ViewModel that return the max RowIndex and ColumnIndex, and in the Grid's Loaded event add however many Columns/Rows you need.

如果代码仅与UI相关,我觉得在MVVM中使用代码隐藏是完全可以接受的。

I find it perfectly acceptable to use code-behind in MVVM IF the code is related to the UI only.

另一个想法是将您的代码隐藏在2D网格中,然后返回到View,然后将该网格绑定到具有的AutoGenerateColumns = True的DataGrid 并删除标题

Another idea would be to arrange your items in your code-behind into a 2D grid before returning it to the View, and then bind that Grid to a DataGrid with AutoGenerateColumns=True and the headers removed

更新

我的当前解决这个问题的解决方案是使用一组 AttachedProperties 为一个 Grid ,允许绑定 RowCount ColumnCount 属性到ViewModel上的属性

My current solution to this problem is to use a set of AttachedProperties for a Grid that allow you to bind RowCount and ColumnCount properties to a property on the ViewModel

您可以找到我的代码版本的附加属性在我的博客上这里,可以这样使用:

You can find the code for my version of the attached properties on my blog here, and they can be used like this:

<ItemsPanelTemplate>
    <Grid local:GridHelpers.RowCount="{Binding RowCount}"
          local:GridHelpers.ColumnCount="{Binding ColumnCount}" />
</ItemsPanelTemplate>

这篇关于WPF Grid为ItemsPanel,用于动态绑定到ItemsControl的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆