我想在UWP中水平和垂直拉伸列表框。我尝试了一些WPF解决方案,但没有一个能够工作。 (Stretch line to width of Itemstemplate canvas in itemscontrol)
我尝试了什么:
<Page.Content>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListBox VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" Background="Green">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
<Setter Property="VerticalAlignment" Value="Stretch"></Setter>
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
<Setter Property="Background" Value="AliceBlue" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Template>
<ControlTemplate TargetType="ListBox">
<ItemsPresenter Height="252" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>asdf</ListBoxItem>
<ListBoxItem>asdfasdf</ListBoxItem>
<ListBoxItem>asdfsdf</ListBoxItem>
<ListBoxItem>34</ListBoxItem>
<ListBoxItem>as2df</ListBoxItem>
<ListBoxItem>asdf</ListBoxItem>
</ListBox>
</Grid>
</Page.Content>
结果如下:
如何在uwp中拉伸列表框?
答案 0 :(得分:1)
您已明确设置Height="252"
。这就是它没有出现的原因。您还可以将实际ListBox的背景设置为Green
,但ItemsPanelTemplate
会覆盖该列表,因此Green
不会显示。
您的最终XAML应如下所示。
<ListBox VerticalContentAlignment="Stretch" VerticalAlignment="Stretch" Background="Green">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
<Setter Property="VerticalAlignment" Value="Stretch"></Setter>
<Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
<Setter Property="Background" Value="AliceBlue" />
</Style>
</ListBox.ItemContainerStyle>
<ListBoxItem>asdf</ListBoxItem>
<ListBoxItem>asdfasdf</ListBoxItem>
<ListBoxItem>asdfsdf</ListBoxItem>
<ListBoxItem>34</ListBoxItem>
<ListBoxItem>as2df</ListBoxItem>
<ListBoxItem>asdf</ListBoxItem>
</ListBox>
这不是经过测试但应该按预期工作。