ListBox模板,隐藏元素

发布于 2024-10-24 00:36:22 字数 2090 浏览 1 评论 0原文

我有一个显示图像和三个文本框的列表框模板。您将在下面的代码中看到它们是如何分布的。我的问题是有些项目没有图像,在这种情况下我希望文本填充整行。 我尝试过不使用网格,而是使用画布,但我不知道为什么,当在列表框中使用画布时,没有显示任何内容。我不知道这是否容易实现。代码如下:

<ListBox Grid.Column="0" Grid.Row="1" Background="White" HorizontalAlignment="Stretch" Name="itemList" VerticalContentAlignment="Stretch" SelectionChanged="listBoxSetmana_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="480">
                <Grid.RowDefinitions>
                    <RowDefinition Height="28" />
                    <RowDefinition Height="17" />
                    <RowDefinition Height="50" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="60" />
                     <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.RowSpan="3" Grid.Column="0" Grid.Row="0" BorderThickness="1" BorderBrush="#FFFF003F" Padding="1">
                    <Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="listImage" Width="36" Height="36" Source="{Binding thumbnail}" />
                </Border>
                <TextBlock Padding="0 0 25 0" Grid.Column="1" Grid.Row="0" Name="title" Foreground="Black" Text="{Binding title}" FontWeight="Bold" FontSize="20" />
                <TextBlock Padding="0 0 25 0" Grid.Column="1" Grid.Row="1" Name="published" Foreground="Black" Text="{Binding published}" FontSize="13" />
                <TextBlock Padding="0 0 25 0" Grid.Column="1" Grid.Row="2" Name="subtitle" Foreground="Black" Text="{Binding subtitle}" FontSize="16" TextWrapping="Wrap" />
                <TextBlock Visibility="Collapsed" Text="{Binding id}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

谢谢!

I have a listbox template showing an image and three textboxes. You will see how are they distributed in the code below. My problem is that some items won't have an image and I want the text to fill the whole row in that case.
I've tried not to use a grid, to use a canvas, but I don't know why, when using a canvas inside a listbox, nothing is shown. I don't know if this is easy to accomplish. Here's the code:

<ListBox Grid.Column="0" Grid.Row="1" Background="White" HorizontalAlignment="Stretch" Name="itemList" VerticalContentAlignment="Stretch" SelectionChanged="listBoxSetmana_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="480">
                <Grid.RowDefinitions>
                    <RowDefinition Height="28" />
                    <RowDefinition Height="17" />
                    <RowDefinition Height="50" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="60" />
                     <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.RowSpan="3" Grid.Column="0" Grid.Row="0" BorderThickness="1" BorderBrush="#FFFF003F" Padding="1">
                    <Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="listImage" Width="36" Height="36" Source="{Binding thumbnail}" />
                </Border>
                <TextBlock Padding="0 0 25 0" Grid.Column="1" Grid.Row="0" Name="title" Foreground="Black" Text="{Binding title}" FontWeight="Bold" FontSize="20" />
                <TextBlock Padding="0 0 25 0" Grid.Column="1" Grid.Row="1" Name="published" Foreground="Black" Text="{Binding published}" FontSize="13" />
                <TextBlock Padding="0 0 25 0" Grid.Column="1" Grid.Row="2" Name="subtitle" Foreground="Black" Text="{Binding subtitle}" FontSize="16" TextWrapping="Wrap" />
                <TextBlock Visibility="Collapsed" Text="{Binding id}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Thank you!

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

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

发布评论

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

评论(1

抹茶夏天i‖ 2024-10-31 00:36:22

有几种方法可以将其绑定在一起。

一种方法是:

  • 向边框添加一些填充,
  • 使用适当的 ValueConverter 将边框的可见性绑定到缩略图属性,
  • 将第 0 列的宽度从“60”更改为“自动” - 然后当图像折叠时它将消失。

There are several ways to bind this together.

One way is:

  • add some Padding to the Border
  • bind the Visibility of the Border to the thumbnail Property using an appropriate ValueConverter
  • change the width of Column 0 from "60" to "Auto" - then it will disappear when the image is Collapsed.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文