C# 要在网格Wpf C中动态附加文本块吗#
我想创建聊天机器人并使用响应网格作为主要聊天区域。每当我使用不同字符串的方法时,每次我的方法都会覆盖上一个文本块,但我想将下一个文本块附加到第一个文本块下面,如whatsapp和任何其他messenger 这是我的xaml代码C# 要在网格Wpf C中动态附加文本块吗#,c#,wpf,grid,chatbot,textblock,C#,Wpf,Grid,Chatbot,Textblock,我想创建聊天机器人并使用响应网格作为主要聊天区域。每当我使用不同字符串的方法时,每次我的方法都会覆盖上一个文本块,但我想将下一个文本块附加到第一个文本块下面,如whatsapp和任何其他messenger 这是我的xaml代码 <Grid x:Name="Responce" HorizontalAlignment="Left" Height="315" VerticalAlignment="Top" Width
<Grid x:Name="Responce" HorizontalAlignment="Left" Height="315" VerticalAlignment="Top" Width="422" Margin="10,10,0,0"/>
在此处输入图像描述
如果我要查看有问题的实际输出则应使用
列表框
和可观察收集
。它更简单,默认情况下允许您滚动内容/消息。将
ListBox.ishitestvisible
设置为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);
}
}
公共部分类主窗口:窗口
{
公共静态只读DependencyProperty SmsCollectionProperty=DependencyProperty.Register(
“SmsCollection”,
类型(可观测采集),
类型(主窗口),
新PropertyMetadata(默认值(ObservableCollection));
公共可观测集合SMS集合
{
get=>(ObservableCollection)GetValue(MainWindow.SmsCollectionProperty);
set=>SetValue(MainWindow.SmsCollectionProperty,value);
}
公共主窗口()
{
初始化组件();
this.smscolection=新的ObservableCollection();
}
私有void CustomerReply(字符串sms)
{
//由于SmsCollection是一个可观察的集合,因此添加/删除/移动将立即反映在UI中。
此.smscolection.Add(sms);
}
}
main window.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>
...
为什么要避免
DataContext
设置?当我使用MainWindow.xaml代码时,它可能看起来比添加DP.MainWindow.xaml更容易显示错误@aepot“{Biding reeactivesource={RelativeSource AncestorType=MainWindow},Path=smscolection}”这里有两个输入错误:更改为“{Binding smscolection,RelativeSource={RelativeSource AncestorType=MainWindow}”
@aepot谢谢您的提示。我已经纠正了错误。我知道数据绑定将在没有依赖属性的情况下工作。但是,由于通常强烈建议将作为绑定源的每个属性都实现为依赖属性
,因此为了提倡良好的编码实践,我选择使用它。使用dependencProperty
提供最佳绑定性能,其次是INotifyPropertyChanged
,并使用普通CLR属性(在某些情况下会导致内存泄漏)。@AhmadKabeer在向构造函数添加以下行时,可以简化绑定:this.DataContext=this代码>。然后,ListBox.ItemsSource
属性上的绑定将变成:
。如果不打算将任何其他数据源用作控件的数据上下文,则这是一个有效的选项。
...
<StackPanel x:Name="RootStack">
</StackPanel>
...
private void CustomerReply(string sms)
{
var textBlock = new TextBlock {Text = sms};
//add other values here
RootStack.Children.Add(textBlock);
}