c# - 如何让动态 ListBox ItemTemplate 水平拉伸(stretch) ListBox 的整个宽度?

标签 c# wpf listbox

我有一个带有自定义模板的普通列表框来显示项目。我无法管理的是在列表框的整个宽度上水平拉伸(stretch)模板。

我的问题是,主窗口中的所有元素都是动态放置的,并且它们会随着窗口大小更改方法的大小而调整。我在网上搜索过,我的想法是放置 Horizo​​ntalAligment="Stretch"。我尽可能地尝试了这一点,但没有取得重大成功。

我的 xaml 代码是这样的:

<UserControl x:Class="LiveGames.Dealer.UsersList"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         Background="Transparent">
<Border CornerRadius="10" BorderBrush="White" BorderThickness="2" >
     <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="PlayerTemplate">
                <WrapPanel>
                    <Border CornerRadius="10" BorderBrush="White" BorderThickness="2" >
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Offset="0.0" x:Name="gradientOne_Name" Color="Red" />
                                <GradientStop Offset="1.0" x:Name="gradientTwo_Name" Color="DarkRed" />
                            </LinearGradientBrush>
                        </Border.Background>
                        <Grid >
                            <Grid.ColumnDefinitions x:Uid="5">
                                <ColumnDefinition  Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Name="RowH" Height="50"/>
                            </Grid.RowDefinitions>

                            <!--  <TextBlock VerticalAlignment="Center" Margin="5" Grid.Column="0" Grid.Row="1" Text="Player: " FontSize="18" />-->
                            <TextBlock VerticalAlignment="Center" Margin="5" Grid.Column="0" Text="{Binding Path=ID}" FontSize="22" FontWeight="Bold"/>
                        </Grid>
                    </Border>
                </WrapPanel>
            </DataTemplate>
        </Grid.Resources>
        <Canvas>
            <ListBox Name="userList" Background="Transparent" BorderBrush="Transparent" BorderThickness="0" ItemsSource="{Binding}"  ItemTemplate="{StaticResource PlayerTemplate}" HorizontalContentAlignment="Stretch">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem" >
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
        </Canvas>
    </Grid>
</Border>

有没有人知道如何做到这一点,甚至可能在后面的代码中?

中号

最佳答案

你可以试试这个:

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="Width" Value="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ScrollContentPresenter}}}"></Setter>
</Style>

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

相关文章:

c# - 如何处理从 CloudPageBlob.BeginUploadFromStream 中引发的异常

c# - WPF 使用附加属性和行为执行 ICommand

所有应用程序窗口的 WPF 图标

wpf - 如何在控件渲染时显示一些动画?

.NET 字典键作为 ListBox 数据源

c# - 将 BitmapImage 转换为灰度,并保留 alpha channel

c# - 在 LocalMachine 存储或 CurrentUser 存储中找不到证书 - 远程证书名称不匹配

c# - 如何从 C# Mongodb 强类型驱动程序中的文档加载特定字段

WPF Grid - 自动对齐它的子项

forms - 向表格添加新行时 Excel 停止工作