c# - WPF ItemTemplate 导致列表框内容大小问题
问题描述
在 WPF 项目中,ListBox
如果我在 XAML 中手动插入项目,则可以正确呈现,例如:
<ListBoxItem>
<Grid Background="#7F271043" Width="200" Height="200">
<Grid.RowDefinitions>
<RowDefinition Height="110" />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="Logo-40p.png" HorizontalAlignment="Center" Margin="10,10,10,10" />
<TextBlock Grid.Row="1" Text="Test Item" Foreground="White" Margin="10,0,10,0" FontSize="16" VerticalAlignment="Center" />
<local:Rating Grid.Row="2" HorizontalAlignment="Left" Height="24" Margin="10,0,0,0" VerticalAlignment="Center" Width="130" SelectedValue="4" IsReadOnly="True" />
<TextBlock Grid.Row="2" Text="Free" Foreground="#FF969292" Margin="0,0,10,0" FontSize="14" HorizontalAlignment="Right" VerticalAlignment="Center" />
</Grid>
</ListBoxItem>
底部ListBoxItem
看起来像:
但是,我换掉的那一刻<ListBoxItem>
:
<ListBox.ItemTemplate>
<DataTemplate>
即使我保持其他所有内容相同,它也会在我的评级控件中产生一个我无法修复的尺寸问题(不响应手动尺寸或拉伸设置):
关于如何解决这个问题的任何想法?做ItemTemplate
/DataTemplate
做一些可能导致控件不遵守手动设置或由它所在的网格行设置的大小的事情?
解决方案
设法解决了这个问题 - XAML 中手动定义的行为似乎与/ListBoxItem
的输出不同。ItemTemplate
DataTemplate
我的评级用户控件有一个指定的高度和宽度,这些高度和宽度被 a 内的实现 XAML(高度/宽度/拉伸)覆盖,ListBoxItem
但不在ItemTemplate
/内DataTemplate
:
mc:Ignorable="d" Height="750" Width="4070" MouseLeave="UserControl_MouseLeave">
所以我改为Height
/Width
改为DesignHeight
/DesignWidth
来解决这个问题:
mc:Ignorable="d" d:DesignHeight="750" d:DesignWidth="4070" MouseLeave="UserControl_MouseLeave">
推荐阅读
- python - Python Pandas 使用 Fastparquet 将 CSV 转换为 Parquet
- c - 如何使 strtok 在令牌末尾包含换行符?
- visual-studio-code - 如何让我的构建在 VSCode 中生成多行问题消息?
- linux - 与 I/O(HID、SPI、GPIO)接口的 Raspberry Pi 程序
- python - 我怎样才能训练一个模型来接受每边的词向量并预测中间词?
- node.js - 部署nodejs+informixDB到heroku
- datatables - 使用数据表中的过滤列表发送多封电子邮件
- javascript - 对于具有固定数量字段的对象,使用一个 setter 方法还是多个方法更好?
- python - 为什么python中没有设置popitem?
- shell - 在 shell 中执行一个名称中包含空格的 python 文件