所以我试图让itemscontrol中的一些项目水平拉伸,当你看一下下面的代码时,你可能会注意到我非常绝望。我简化了我的代码并添加了一些颜色,以便更好地了解控件的开始和结束位置......是否有可能告诉项目同样拉伸并填充整个水平空间?
<Grid Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ItemsControl x:Name="HorizontalListBox" Background="Blue"
HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch">
<ItemsControl.Items>
<System:Int32>4</System:Int32>
<System:Int32>4</System:Int32>
<System:Int32>4</System:Int32>
<System:Int32>4</System:Int32>
<System:Int32>4</System:Int32>
</ItemsControl.Items>
<ItemsControl.Template>
<ControlTemplate>
<ItemsPresenter HorizontalAlignment="Stretch"/>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Background="Green"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<TextBlock Text="Test" HorizontalAlignment="Stretch"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
看起来像这样:this http://image-upload.de/image/0TgZx9/0dd27b5326.png
然而,我希望有可能让绿色物品遍布整个地方。编辑:感谢下面答案中的博客文章,它进行了一些调整:
protected override Size MeasureOverride(Size availableSize)
{
var size = new Size();
SetSide(ref size, GetSide(availableSize));
foreach (UIElement child in Children)
{
child.Measure(availableSize);
SetOtherSide(ref size, Math.Max(GetOtherSide(size), GetOtherSide(child.DesiredSize)));
}
_measureSize = size;
return size;
}
protected override Size ArrangeOverride(Size finalSize)
{
double offset = 0;
foreach (UIElement child in Children)
{
double side = GetSide(_measureSize) / Children.Count;
var final = new Size();
SetSide(ref final, side);
SetOtherSide(ref final, GetOtherSide(finalSize));
child.Arrange(new Rect(GetOffsetPoint(offset), final));
offset += side;
}
return finalSize;
}
答案 0 :(得分:1)
我认为你使用StackPanel
导致缺乏伸展性。如果您使用其他类型的控件替换面板模板,它将遵循HorizontalAlignment="Stretch"
请求。
答案 1 :(得分:1)
听起来你需要StretchPanel
。 Silverlight没有这样的面板,但你可以自己编写。
我通过谷歌StretchPanel blog找到了一个,只是快速查看代码,它对我来说没问题。
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StretchPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>