如何自动调整容器元素大小以确保可见性问题的最小高度?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
布局元素有很多,虽然这为您提供了很多选择,但很难决定在任何给定情况下使用哪个布局元素是正确的。
一般来说,
Canvas
因其方便的固定定位功能而很有用。它允许使用 Canvas.Left 和 Canvas.Top 附加属性将任何东西放置在任何地方。但由于Canvas
的大小是固定的并且不依赖于其子级,因此很难用于可变大小的内容。Canvas
的父级与Canvas
子级的大小“隔离”,这在某些情况下实际上很有用。相比之下,
Grid
是迄今为止灵活的布局元素,对于使用行和列、带或不带跨越等布局网格非常有用。这就是为什么当您创建新的 <代码>窗口或<代码>用户控件。但与Canvas
不同,Grid
的大小在未指定且未拉伸到可用空间时,是其所有子项大小的并集。Grid
还具有这样的属性:如果它有多个子项,并且它们没有放置在行或列中,那么它们会重叠在彼此之上,后面的子项也将被放置在其中。 Z 顺序较高。这就像一个Canvas
,但是没有Canvas.Left
和Canvas.Top
,我们如何精细地控制孩子的位置?让我们看一个例子。这是一个
Canvas
,其中两个矩形并排放置,并留有一点空间,还有一个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 theCanvas.Left
andCanvas.Top
attached properties. But because the size of aCanvas
is fixed and doesn't depend on its children, it's difficult to use for variable-sized content. The parent of theCanvas
is "insulated" from the size of the children of theCanvas
, 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 newWindow
orUserControl
. But unlike aCanvas
, the size ofGrid
, 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 aCanvas
but withoutCanvas.Left
andCanvas.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 aGrid
doing the same thing:In the first case we had to specific the height of the
Canvas
because it doesn't auto-size. In the second case, we usedMargin
to simulate absolute positioning and theGrid
size adapts to the size of its content.