如何让 ListBox 项目填满整个空间?

发布于 2024-12-12 02:40:09 字数 2184 浏览 0 评论 0原文

您好,我发现有很多关于 ListBox 和填充 DataTemplate 的整个空间的问题,但我仍然无法让任何东西工作。我有一个列表框,DataTemplate 有一个用户控件。如何让我的用户控件拉伸数据以填充空间?

MainWindow XAML 片段:

<ListBox x:Name="ConfiguredItemsList">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ConfiguredItem DataContext="{Binding}" HorizontalContentAlignment="Stretch" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

UserControl XAML 片段:

<UserControl.Resources>
    <local:ImagePathConverter x:Key="ImagePathConverter"/>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="75" />
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" x:Name="NameLabel" Text="Name:" />
        <TextBlock Grid.Row="0" Grid.Column="1" x:Name="tbName" Text="{Binding Path=Name}" />
        <TextBlock Grid.Row="1" Grid.Column="0" x:Name="DescriptionLabel" Text="Description: " />
        <TextBlock Grid.Row="1" Grid.Column="1" x:Name="Description" Text="{Binding Path=Description}" />
        <TextBlock Grid.Row="2" Grid.Column="0" x:Name="TimeLabel" Text="Time:" />
        <TextBlock Grid.Row="2" Grid.Column="1" x:Name="Time" Text="{Binding Path=ChangeTime, StringFormat={}{0:h:mm tt}}" />            
    </Grid>

    <Border Grid.Column="1" BorderThickness="1" BorderBrush="Black">
        <Image Source="{Binding Path=WallpaperInfo, Converter={StaticResource ImagePathConverter}}" />
    </Border>
</Grid>

Hi I see there have been many questions about ListBox and filling the entire space for the DataTemplate but I still cannot get anything to work. What I have is a ListBox and the DataTemplate has a UserControl. How do I get my UserControl to stretch the data to fill the space?

MainWindow XAML snippet:

<ListBox x:Name="ConfiguredItemsList">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ConfiguredItem DataContext="{Binding}" HorizontalContentAlignment="Stretch" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

UserControl XAML snippet:

<UserControl.Resources>
    <local:ImagePathConverter x:Key="ImagePathConverter"/>
</UserControl.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="75" />
    </Grid.ColumnDefinitions>
    <Grid Grid.Column="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0" Grid.Column="0" x:Name="NameLabel" Text="Name:" />
        <TextBlock Grid.Row="0" Grid.Column="1" x:Name="tbName" Text="{Binding Path=Name}" />
        <TextBlock Grid.Row="1" Grid.Column="0" x:Name="DescriptionLabel" Text="Description: " />
        <TextBlock Grid.Row="1" Grid.Column="1" x:Name="Description" Text="{Binding Path=Description}" />
        <TextBlock Grid.Row="2" Grid.Column="0" x:Name="TimeLabel" Text="Time:" />
        <TextBlock Grid.Row="2" Grid.Column="1" x:Name="Time" Text="{Binding Path=ChangeTime, StringFormat={}{0:h:mm tt}}" />            
    </Grid>

    <Border Grid.Column="1" BorderThickness="1" BorderBrush="Black">
        <Image Source="{Binding Path=WallpaperInfo, Converter={StaticResource ImagePathConverter}}" />
    </Border>
</Grid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房敞 2024-12-19 02:40:09

您是否尝试过将 Horizo​​ntalContentAlignment 设置为在列表框级别上拉伸?

<ListBox x:Name="ConfiguredItemsList"  HorizontalContentAlignment="Stretch">
      <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ConfiguredItem DataContext="{Binding}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

注意:我设置了 ListBoxHorizo​​ntalContentAlignment 属性,而不是 Item

Have you tried to set HorizontalContentAlignment to stretch on list box level?

<ListBox x:Name="ConfiguredItemsList"  HorizontalContentAlignment="Stretch">
      <ListBox.ItemTemplate>
            <DataTemplate>
                <local:ConfiguredItem DataContext="{Binding}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

Note: I have set HorizontalContentAlignment property of ListBox instead of Item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文