我正在创建一个类似网格的控件,并且使用一个 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)还有其他的可以表示2D数据吗? (我正在使用 silverlight 4 MVVM)
提前致谢。
最佳答案
在此处查看 SL 控件的 UniformGrid:http://www.jeff.wilcox.name/2009/01/uniform-grid/
MSDN 中 UniformGrid 的定义:“提供了一种在网格中排列内容的方法,其中网格中的所有单元格都具有相同的大小。”
如果您希望将其绑定(bind)到可观察集合,只需将 UniformGrid 放置在 ItemsControl 的 ItemsPanelTemplate 中,因为 UniformGrid 扩展了 Panel。
关于mvvm - 项目控件水平放置项目并使子项拉伸(stretch)到堆栈面板宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9431280/