简体   繁体   English

我怎样才能将串口的传入字符串 append 输入到文本框中?

[英]How can I append the incoming strings of a serial port to a text box?

I am a c# sharp beginner and my first project is a terminal application for the serial port.我是 c# 敏锐的初学者,我的第一个项目是串行端口的终端应用程序。 The incoming data are ascii characters and they come with 115 kbaud.传入的数据是 ascii 字符,它们带有 115 kbaud。

Now I want to bind each incoming line to a wpf text box.现在我想将每条传入线路绑定到 wpf 文本框。 In my serial port class I use IPropertyChanged events and I succeeded in binding each incoming line to the textbox.在我的串行端口 class 中,我使用 IPropertyChanged 事件,并且成功地将每个传入行绑定到文本框。 But it's not satisfying just to see one line, the content of the textbox should be appended with each incoming line.但是仅仅看到一行并不令人满意,文本框的内容应该附加到每一行。 I don't want to collect the strings in my serial port class and just send them all again with each new one.我不想在我的串行端口 class 中收集字符串,然后将它们与每个新字符串一起再次发送。 I tried and the result was very slow.我试过了,结果很慢。

Next I tried to use the binding event TargetUpdated to use textbox.AppendText(incomingline)...接下来我尝试使用绑定事件 TargetUpdated 来使用 textbox.AppendText(incomingline)...

<TextBox 
Name="textBoxIncoming" 
Text="{Binding Path=Incoming, Mode=OneWay, NotifyOnTargetUpdated=True}" 
DataContext="{Binding NotifyOnTargetUpdated=True}"
IsReadOnly="True"
TargetUpdated="textBoxIncoming_TargetUpdated" 
... />`

The result was that each incoming line is displayed twice in the text box, the lines before are cleared.结果是每个传入的行在文本框中显示两次,之前的行被清除。

Does anybody know, how to append text to a textbox with wpf binding?有谁知道,如何将 append 文本发送到具有 wpf 绑定的文本框?

This cannot be done with a binding alone.这不能仅通过绑定来完成。 Instead, create a List where you add all lines that you receive.相反,创建一个列表,您可以在其中添加您收到的所有行。 Modify the Incoming property to return string.Join(Environment.NewLine, listOfReceivedStrings).修改 Incoming 属性以返回 string.Join(Environment.NewLine, listOfReceivedStrings)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM