想要动态地在网格中附加文本块 Wpf C#

want to append text-block in grid dynamically Wpf C#

我想创建 Chatbot 并使用 Responce 网格作为主聊天 area.whenever 我使用我的方法和不同的字符串然后每次我的方法覆盖前一个文本块但我想将我的下一个文本块附加到第一个文本块下面喜欢 whatsapp 和任何其他信使。

那是我的 xaml 代码

<Grid x:Name="Responce" HorizontalAlignment="Left" Height="315" VerticalAlignment="Top" Width="422" Margin="10,10,0,0"/>

这是我的 c# 方法,它创建文本块并将其添加到网格中。

private void CustomerReply(string sms)
        {
            StackPanel sp = new StackPanel();
            var textBlock = new TextBlock();
            var bc = new BrushConverter();
            textBlock.Text = sms;          
            textBlock.HorizontalAlignment = HorizontalAlignment.Right;
            textBlock.VerticalAlignment = VerticalAlignment.Top;
            textBlock.FontSize = 12;
            textBlock.Foreground = new SolidColorBrush(Colors.Black);
            textBlock.TextAlignment = TextAlignment.Left;
            textBlock.Background = (Brush)bc.ConvertFrom("#e0fcc4");
            textBlock.FontFamily = new FontFamily("Helvetica Neue");
            textBlock.FontStretch = FontStretches.UltraExpanded;
            textBlock.FontStyle = FontStyles.Normal;
            textBlock.Typography.NumeralStyle = FontNumeralStyle.OldStyle;
            textBlock.Typography.SlashedZero = true;
            sp.Children.Add(textBlock);
            Responce.Children.Add(sp);

在此处输入图片描述 我的实际输出有问题

您应该改用 ListBoxObservableCollection。它更容易,默认情况下允许您滚动 content/messages。
ListBox.IsHitTestVisible 设置为 False 将禁用对 ListBox 项的选择并删除列表外观。

MainWindow.xaml.cs

public partial class MainWindow : Window
{
  public static readonly DependencyProperty SmsCollectionProperty = DependencyProperty.Register(
    "SmsCollection",
    typeof(ObservableCollection<string>),
    typeof(MainWindow),
    new PropertyMetadata(default(ObservableCollection<string>)));

  public ObservableCollection<string> SmsCollection
  {
    get => (ObservableCollection<string>) GetValue(MainWindow.SmsCollectionProperty); 
    set => SetValue(MainWindow.SmsCollectionProperty, value); 
  }

  public MainWindow()
  {
    InitializeComponent();
    this.SmsCollection = new ObservableCollection<string>();
  }

  private void CustomerReply(string sms)
  {
    // Since SmsCollection is a ObservablecCollection, add/remove/move will be reflected in the UI immediately.
    this.SmsCollection.Add(sms);
  }
}

MainWindow.xaml

<Window>
  <ListBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=MainWindow}, Path=SmsCollection}" 
           IsHitTestVisible="False">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <TextBlock Text="{Binding}"
                   Background="#e0fcc4"
                   FontFamily="Helvetica Neue"
                   ...
                       />
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>
</Window>

XAML:

...
<StackPanel x:Name="RootStack">
</StackPanel>
...

后面的代码:

private void CustomerReply(string sms)
{
    var textBlock = new TextBlock {Text = sms};
    //add other values here
    RootStack.Children.Add(textBlock);
}