如何动态更改 ListBoxItem UWP 的高度?

How to dynamically change the height of ListBoxItem UWP?

我需要你的帮助。

此时我正在制作一个通知面板。我需要更改 ListBoxItem 的高度以响应许多消息行。 Here's an example。 如您所见,第一个通知并没有增加它的高度,并且带有时间码的 TextBlock 刚刚消失。

这是我的代码示例:

<ListBox Width="350" Padding="0,0,0,10" Name="notificationArray">
    <ListBoxItem Padding="10" MinHeight="80" Height="Auto" VerticalAlignment="Stretch">
        <StackPanel>
            <TextBlock FontSize="14" FontStyle="Italic" Foreground="Gray" Text="Channel name"/>
            <TextBlock TextWrapping="WrapWholeWords" Text="This is very long notification. One, two, three, four, five, six, seven, eight, nine, ten."/>
            <TextBlock Foreground="Gray" Text="HH:MM TT" FontSize="13"/>
        </StackPanel>
    </ListBoxItem>
</ListBox>

如您所见,我已经尝试了 Height="Auto"VerticalAlignment="Stretch",但仍然无效。有什么建议么?无论如何,先谢谢了。

P.S。问题肯定在ListBoxItem。我已经尝试过 ListBoxStackPanel

P.P.S 这是一个更大的样本:

<Popup Grid.Column="6" Margin="0,50,0,0" Name="notificationPane" LostFocus="notificationPane_LostFocus" IsOpen="False" IsLightDismissEnabled="True">
    <StackPanel Width="350" Background="WhiteSmoke" Padding="0,10,0,0">
        <TextBlock Text="Notifications" HorizontalAlignment="Center" FontWeight="Bold"/>
        <StackPanel Margin="0,10,0,0">
            <TextBlock Text="You have no any notification" Name="noNotifText" Foreground="Gray" FontStyle="Italic" Padding="10" Visibility="Visible"/>
            <StackPanel Name="notificationPanel" Visibility="Collapsed">
                <ListBox Width="350" Padding="0,0,0,10" Name="notificationArray">
                    <ListBoxItem Padding="10" MinHeight="80">
                        <StackPanel>
                            <TextBlock FontSize="14" FontStyle="Italic" Foreground="Gray" Text="Channel name"/>
                                <TextBlock TextWrapping="WrapWholeWords" Text="This is very long notification. One, two, three, four, five, six, seven, eight, nine, ten."/>
                                <TextBlock Foreground="Gray" Text="HH:MM TT" FontSize="13"/>
                            </StackPanel>
                    </ListBoxItem>
                    <ListBoxItem Padding="10" VerticalAlignment="Stretch" Background="LightGray" MinHeight="80" Height="100">
                        <StackPanel>
                            <TextBlock FontSize="14" FontStyle="Italic" Foreground="Gray" Text="Channel name"/>
                            <TextBlock TextWrapping="WrapWholeWords" Text="This is very long notification. One, two, three, four, five, six, seven, eight, nine, ten."/>
                            <TextBlock Foreground="Gray" Text="HH:MM TT" FontSize="13"/>
                        </StackPanel>
                    </ListBoxItem>
                </ListBox>
                <Button Content="Clear all" HorizontalAlignment="Right" Margin="10,0,10,10"/>
            </StackPanel>
        </StackPanel>
    </StackPanel>
</Popup>

我从您上传到 GitHub 的开发分支下载了您的整个项目并找到了问题所在。由于您在项目的 App.xaml 中定义了 ListBoxItem 的全局样式,因此将 ListBoxItemHeight 属性 设置为 50 即限制 ListBoxItem 显示内容。

要解决您的问题,请评论此样式,或者像为第二个一样为第一个 ListBoxItem 设置固定高度,或者为第一个项目设置更大的 MinHeight

<Application.Resources>
    <!--<Style TargetType="ListBoxItem">
        <Setter Property="Height" Value="50"/>
    </Style>-->
    <Style TargetType="TextBlock" x:Key="ItemIcon">
        <Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
        <Setter Property="FontSize" Value="16"/>
        <Setter Property="Margin" Value="5,5,20,2"/>
    </Style>
...
</Application.Resources>