项目控件水平放置项目并使子项拉伸到堆栈面板宽度

发布于 2025-01-08 09:51:18 字数 1329 浏览 3 评论 0原文

我正在创建一个类似网格的控件,并且使用一个 itemscontrol 来表示一行,并使用另一个 itemscontrol 将这些行放在一起。在一行中,项目水平放置,因此我使用具有水平方向的 StackPanel,如下所示:

<ItemsControl ItemsSource="{Binding CellRows}" Grid.Row="1" Grid.Column="1" Margin ="20">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" Background="Cyan"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Content}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

默认情况下,我希望我的 StackPanel 采用其父级的大小,并且这种情况发生了。但我也希望 itemscontrol 中的所有子项(将 stackpanel 作为 itemspanel)占据其父项的整个宽度。但这并没有发生。

我读过这也是堆栈面板的默认行为。

我的问题:

1)有什么方法可以让我的文本框(子级)占据我的堆栈面板(父级)的整个宽度?我不知道堆栈面板将包含多少个项目,因此我无法为文本框指定默认宽度。

2)还有其他的方式来表示二维数据吗? (我正在使用 silverlight 4 MVVM)

提前致谢。

I'm creating a grid like control, and i'm using an itemscontrol to represent a row and the another itemscontrol to put those rows together. In a row, the items are positioned horizontally so I use a StackPanel with a horizontal orientation like this:

<ItemsControl ItemsSource="{Binding CellRows}" Grid.Row="1" Grid.Column="1" Margin ="20">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" Background="Cyan"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Content}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

By default, I want my StackPanel to take the size its parent has and that happens. But I also want all the children in the itemscontrol (with the stackpanel as itemspanel) to take up the full width of its parent. But that doesn't happen.

I've read that is default behavior of a stackpanel as well.

My questions:

1) Is there any way to make my textboxes (children) to take up the full width of my stackpanel (parent)? I don't know how many items the stackpanel will contain, so I can't give a default width to the textboxes.

2) Is there any other to represent 2D data? (I'm working with silverlight 4 MVVM)

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你另情深 2025-01-15 09:51:18

在此处查看 SL 控件的 UniformGrid:http://www.jeff.wilcox .name/2009/01/uniform-grid/

MSDN 中 UniformGrid 的定义:“提供了一种在网格中排列内容的方法,其中网格中的所有单元格都具有相同的大小。”

如果您希望将其绑定到可观察集合,只需将 UniformGrid 放置在 ItemsControl 的 ItemsPanelTemplate 中,因为 UniformGrid 扩展了 Panel。

Check out the UniformGrid for SL control here: http://www.jeff.wilcox.name/2009/01/uniform-grid/

The definition of the UniformGrid from the MSDN: "Provides a way to arrange content in a grid where all the cells in the grid have the same size."

If you want this to be bound to an observable collection simply place the UniformGrid in the ItemsPanelTemplate of an ItemsControl as UniformGrid extends Panel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文