gpt4 book ai didi

xaml - 如何让 ListBox ItemTemplate 水平拉伸(stretch) ListBox 的整个宽度?

转载 作者:行者123 更新时间:2023-12-03 03:58:14 24 4
gpt4 key购买 nike

我想让 ListItems 的橙色背景扩展到列表框的整个宽度。

目前它们的宽度仅为名字 + 姓氏。

我已将所有元素设置为:Horizo​​ntalAlignment="Stretch"。

我希望 ListboxItems 的背景随着用户拉伸(stretch)列表框而展开,因此我不想输入绝对值。

我必须做什么才能使 ListBoxItems 的背景颜色填充 ListBox 的宽度?

<Window x:Class="TestListBoxSelectedItemStyle.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestListBoxSelectedItemStyle"
Title="Window1" Height="300" Width="300">

<Window.Resources>
<local:CustomerViewModel x:Key="TheDataProvider"/>

<DataTemplate x:Key="CustomerItemTemplate">
<Border CornerRadius="5" Background="Orange" HorizontalAlignment="Stretch" Padding="5" Margin="3">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Width="Auto">
<TextBlock HorizontalAlignment="Stretch">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</Border>
</DataTemplate>

</Window.Resources>

<Grid>
<ListBox ItemsSource="{Binding Path=GetAllCustomers, Source={StaticResource TheDataProvider}}"
ItemTemplate="{StaticResource CustomerItemTemplate}"/>
</Grid>
</Window>

最佳答案

我找到了another solution here ,因为我遇到了这两篇文章......

这是来自迈尔斯的回答:

<ListBox.ItemContainerStyle> 
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>

这对我有用。

关于xaml - 如何让 ListBox ItemTemplate 水平拉伸(stretch) ListBox 的整个宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/838828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com