创建一个方形边框,内部有图像,宽度和高度取决于统一的网格列
我认为这是一个有点棘手的问题。
我有一个列表框,必须显示一些图像,并且每个图像必须放在矩形或方形边框内。这很简单,我知道。事实上,无论屏幕分辨率还是窗口大小,此列表框每行必须始终显示 3 个元素。
为了获得它,我已经模板化了列表框,如下所示:
<ListBox Grid.Column="2" Grid.Row="1" x:Name="_productsLB" SelectedIndex="0"
ItemsSource="{Binding Products}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalContentAlignment="Stretch">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" Rows="4"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" Background="White" BorderBrush="Black" Margin="8"
Width="Auto" Height="Auto" HorizontalAlignment="Stretch">
<Image Source="{Binding ImagePath}" Stretch="Uniform"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
问题是我在图像周围获得了矩形边框,而不是列表框项“周围”的方形边框。
请记住,我无法指定 width 属性,因为它们必须依赖于统一的网格列宽度。
你有一些提示吗?
太感谢了!
I think this is a little tricky problem.
I have a ListBox that must present some images and each image must be put inside a rectangle or a square border. This is simple, I know. The fact is that this listbox must show always 3 elements per row, whether the screen resolution or window size.
In order to obtain it I've templated the listbox like this:
<ListBox Grid.Column="2" Grid.Row="1" x:Name="_productsLB" SelectedIndex="0"
ItemsSource="{Binding Products}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalContentAlignment="Stretch">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" Rows="4"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" Background="White" BorderBrush="Black" Margin="8"
Width="Auto" Height="Auto" HorizontalAlignment="Stretch">
<Image Source="{Binding ImagePath}" Stretch="Uniform"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The problem is that I obtain a rectangular border around the image instead of a square border "around" the listboxitem.
Remember that I can't specifiy width property because they must be dependent to the uniform grid column width.
Do you have some hints?
Thank you so much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从边框中删除边距并将其设置在图像上,在图像周围创建一个与 ListviewItem 一样大的边框。看图片。
如果这不是您所期望的,请更清楚地定义。
编辑:
将
VerticalContentAlignment="Stretch"
添加到列表框中,图像应该正确调整大小。见下图。编辑 II:
如果您希望图像显示为正方形,则必须将边框的高度设置为其当前宽度:
Removing the Margin from the border and setting it on the Image, creates a Border around the Image which is exactly as big as a ListviewItem is. See picture.
If that is not what you expected, please define more clearly.
EDIT:
Add
VerticalContentAlignment="Stretch"
to your ListBox and the images should resize properly. See picture below.EDIT II:
If you want the images to appear as squares, you have to set the Border's Height to its current Width:
试试这个
Try this