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

时间:2018-04-03 19:40:10

标签: xaml uwp windows-10-universal uwp-xaml

我需要你的帮助。

此刻我正在制作一个通知小组。我需要更改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>

1 个答案:

答案 0 :(得分:0)

我从您上传到GitHub的开发分支下载整个项目并找到问题。由于您已定义了项目App.xamlListBoxItem的全局样式,因此将Height的{​​{1}}属性设置为ListBoxItem,限制50 1}}显示内容。

要解决您的问题,请注释此样式,或为第一个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>