C# WPF 列表框聊天

C# WPF ListBox Chat

我正在开发 C# WPF 聊天应用程序。我希望它看起来像 Skype 聊天,所以最后添加的项目触及列表框的底部。

截图:

<ListBox Name="ListBoxMain" Grid.Column="1" Grid.Row="1">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <WrapPanel>
                    <TextBlock Text="{Binding User}" FontWeight="Bold" />
                    <TextBlock Text=": " FontWeight="Bold" />
                    <TextBlock Text="{Binding Text}" Width="225" TextWrapping="Wrap" HorizontalAlignment="Stretch" />
                </WrapPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

您需要更新 ListBox 的内部 ItemsPanelTemplate...试试这个:

<ListBox Name="ListBoxMain" Grid.Column="1" Grid.Row="1">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <TextBlock Text="{Binding User}" FontWeight="Bold" />
                <TextBlock Text=": " FontWeight="Bold" />
                <TextBlock Text="{Binding Text}" Width="225" TextWrapping="Wrap" HorizontalAlignment="Stretch" />
            </WrapPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel VerticalAlignment="Bottom"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>