WPF ItemTemplate导致列表框内容大小问题

问题描述

在WPF项目中,如果我在XAML中手动插入项目,则我的ListBox可以正确呈现,例如:

<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" />
        <TextBlock Grid.Row="1" Text="Test Item" Foreground="White" Margin="10,0" FontSize="16" VerticalAlignment="Center" />
        <local:Rating Grid.Row="2" HorizontalAlignment="Left" Height="24" Margin="10,0" VerticalAlignment="Center" Width="130" SelectedValue="4" IsReadOnly="True" />
        <TextBlock Grid.Row="2" Text="Free" Foreground="#FF969292" Margin="0,0" FontSize="14" HorizontalAlignment="Right" VerticalAlignment="Center" />
    </Grid>
</ListBoxItem>

ListBoxItem的底部看起来像:

Manual Item Insertion

但是,当我换出<ListBoxItem>的那一刻:

<ListBox.ItemTemplate>
    <DataTemplate>

即使我保持其他所有条件不变,它也会导致我无法修复的等级控制出现大小调整问题(不响应手动尺寸或拉伸设置):

ItemTemplate

关于如何解决此问题的任何想法? ItemTemplate / DataTemplate所做的事情是否可能导致控件不遵守手动设置的大小或由其所在的网格行设置的大小?

解决方法

设法解决此问题-XAML中手动定义的ListBoxItem的行为与ItemTemplate / DataTemplate的输出有所不同。

我的评分用户控件指定了设置的高度和宽度,这些高度和宽度被ListBoxItem内的实现XAML(高度/宽度/拉伸)覆盖,但未被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">

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...