如何自动调整容器元素大小以确保可见性问题的最小高度?

发布于 2024-11-17 04:00:16 字数 2284 浏览 1 评论 0原文

我有一个 ItemsControl,想要显示我的 CustomObject 中的一些字符串。

这就像

String A
String B
String C

字符串 A 和 B 可以有多行长,但 C 不能。我正在考虑 Height="Auto" 和 DockPanel。弦 A 的高度应该是它需要的高度。还有弦 B。

这是我到目前为止想到的:

<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Background="Black">
    <ItemsControl Name="ItemsControl1">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="#FF126eb1" BorderThickness="1.5" CornerRadius="8,8,8,8" Background="#FF074e84" Width="350" Height="Auto">
                    <DockPanel Width="350" Margin="0,10,0,0" Height="Auto" Background="Transparent">
                        <Canvas DockPanel.Dock="Top" Height="Auto" Width="350" Margin="0,10,0,0">
                            <TextBlock Text="{Binding Headline}" Canvas.Left="5" Canvas.Top="5" Foreground="White" FontSize="15" FontWeight="Bold" MaxWidth="340" TextWrapping="Wrap" Height="Auto"/>
                        </Canvas>
                        <Canvas DockPanel.Dock="Top" Height="Auto" Width="350" Margin="0,10,0,0">
                            <TextBlock Text="{Binding Description}" Canvas.Left="5" Canvas.Top="20" Foreground="White" FontSize="13" MaxWidth="340" TextWrapping="Wrap" Height="Auto" />
                        </Canvas>
                        <Canvas DockPanel.Dock="Top" Width="350" Height="40" Margin="0,10,0,0" Background="Transparent">
                            <TextBlock Text="{Binding DeadlineOn, StringFormat='Deadline: {0}'}" Canvas.Left="5" Canvas.Top="5" Foreground="White"/>
                             <!-- and other controls -->
                        </Canvas>
                    </DockPanel>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

不幸的是,只有 margin 属性为字符串 A 和 B 提供高度。

如果每个项目的高度未知,我该怎么做?

I have an ItemsControl and want to display some Strings from my CustomObject.

It's like

String A
String B
String C

where String A and B can be multiple lines long, but C can't. I was thinking of Height="Auto" and a DockPanel. The height of String A should be however it needs to be. With String B as well.

This is what I came up with so far:

<ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" Background="Black">
    <ItemsControl Name="ItemsControl1">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="#FF126eb1" BorderThickness="1.5" CornerRadius="8,8,8,8" Background="#FF074e84" Width="350" Height="Auto">
                    <DockPanel Width="350" Margin="0,10,0,0" Height="Auto" Background="Transparent">
                        <Canvas DockPanel.Dock="Top" Height="Auto" Width="350" Margin="0,10,0,0">
                            <TextBlock Text="{Binding Headline}" Canvas.Left="5" Canvas.Top="5" Foreground="White" FontSize="15" FontWeight="Bold" MaxWidth="340" TextWrapping="Wrap" Height="Auto"/>
                        </Canvas>
                        <Canvas DockPanel.Dock="Top" Height="Auto" Width="350" Margin="0,10,0,0">
                            <TextBlock Text="{Binding Description}" Canvas.Left="5" Canvas.Top="20" Foreground="White" FontSize="13" MaxWidth="340" TextWrapping="Wrap" Height="Auto" />
                        </Canvas>
                        <Canvas DockPanel.Dock="Top" Width="350" Height="40" Margin="0,10,0,0" Background="Transparent">
                            <TextBlock Text="{Binding DeadlineOn, StringFormat='Deadline: {0}'}" Canvas.Left="5" Canvas.Top="5" Foreground="White"/>
                             <!-- and other controls -->
                        </Canvas>
                    </DockPanel>
                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>

Unfortunately only the margin property is making height for String A and B.

How can I do this, if the height of each item is unknown?

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

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

发布评论

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

评论(1

迷你仙 2024-11-24 04:00:16

布局元素有很多,虽然这为您提供了很多选择,但很难决定在任何给定情况下使用哪个布局元素是正确的。

一般来说,Canvas 因其方便的固定定位功能而很有用。它允许使用 Canvas.Left 和 Canvas.Top 附加属性将任何东西放置在任何地方。但由于 Canvas 的大小是固定的并且不依赖于其子级,因此很难用于可变大小的内容。 Canvas 的父级与 Canvas 子级的大小“隔离”,这在某些情况下实际上很有用。

相比之下, Grid 是迄今为止灵活的布局元素,对于使用行和列、带或不带跨越等布局网格非常有用。这就是为什么当您创建新的 <代码>窗口或<代码>用户控件。但与 Canvas 不同,Grid 的大小在未指定且未拉伸到可用空间时,是其所有子项大小的并集。

Grid 还具有这样的属性:如果它有多个子项,并且它们没有放置在行或列中,那么它们会重叠在彼此之上,后面的子项也将被放置在其中。 Z 顺序较高。这就像一个Canvas,但是没有Canvas.LeftCanvas.Top,我们如何精细地控制孩子的位置?

让我们看一个例子。这是一个 Canvas,其中两个矩形并排放置,并留有一点空间,还有一个 Grid 做同样的事情:

<Grid>
    <StackPanel>
        <Canvas Height="120">
            <Rectangle Canvas.Left="10" Canvas.Top="10" Height="100" Width="100" Fill="Red"/>
            <Rectangle Canvas.Left="120" Canvas.Top="10" Height="100" Width="100" Fill="Green"/>
        </Canvas>
        <Grid HorizontalAlignment="Left">
            <Rectangle Margin="10,10,10,10" Height="100" Width="100" Fill="Red" HorizontalAlignment="Left"/>
            <Rectangle Margin="120,10,10,10" Height="100" Width="100" Fill="Green" HorizontalAlignment="Left"/>
        </Grid>
    </StackPanel>
</Grid>

在第一种情况下,我们必须指定高度Canvas 的大小,因为它不会自动调整大小。在第二种情况下,我们使用Margin模拟绝对定位,并且Grid大小适应其内容的大小。

There are a lot of layout elements and while this gives you a lot of choices, it can be hard to decide which layout element is the right one to use in any given situation.

In general, a Canvas is useful for its convenient fixed positioning capabilities. It allows anything to be placed anywhere using the Canvas.Left and Canvas.Top attached properties. But because the size of a Canvas is fixed and doesn't depend on its children, it's difficult to use for variable-sized content. The parent of the Canvas is "insulated" from the size of the children of the Canvas, and this is actually useful in some situations.

By contrast, a Grid is by far the flexible layout element and is useful for layout out grids with rows and columns, with or without spanning, etc. This is why its the default when you create a new Window or UserControl. But unlike a Canvas, the size of Grid, when unspecified and not stretched to the available space, is the union of the sizes of all its children.

A Grid also has the property that if it has several children and they aren't placed into rows or columns, then they are overlayed on top of each other, with later children being higher in the Z-order. This is just like a Canvas but without Canvas.Left and Canvas.Top, how can we finely control the position of the children?

Let's look at an example. Here is a Canvas with two rectangles placed side-by-side with a little space, and a Grid doing the same thing:

<Grid>
    <StackPanel>
        <Canvas Height="120">
            <Rectangle Canvas.Left="10" Canvas.Top="10" Height="100" Width="100" Fill="Red"/>
            <Rectangle Canvas.Left="120" Canvas.Top="10" Height="100" Width="100" Fill="Green"/>
        </Canvas>
        <Grid HorizontalAlignment="Left">
            <Rectangle Margin="10,10,10,10" Height="100" Width="100" Fill="Red" HorizontalAlignment="Left"/>
            <Rectangle Margin="120,10,10,10" Height="100" Width="100" Fill="Green" HorizontalAlignment="Left"/>
        </Grid>
    </StackPanel>
</Grid>

In the first case we had to specific the height of the Canvas because it doesn't auto-size. In the second case, we used Margin to simulate absolute positioning and the Grid size adapts to the size of its content.

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