[英]C# appending data in textbox
我正在嘗試使用MVVM將數據附加到文本框中。 我的問題是我的數據沒有追加。 這是我的代碼:
模型:
//This is where the Outgoing Ports are Tapped and the data is displayed to the serial Monitor.
var rawPacket = e.Value as RawPacket; // Data recived from the port after tapped
StringBuilder sb = new StringBuilder();
for (int i = 0; i < rawPacket.RawData.Length; i++) {
sb.Append(rawPacket.RawData[i].ToString("X2"));
}
this.serialData.LuminRecevied = sb.ToString() + Environment.NewLine;
查看模型:
public string LuminRecevied {
get { return luminRecevied; }
set {
if (this.luminRecevied == value) {
return;
}
this.luminRecevied = value ;
this.InvokePropertyChanged("LuminRecevied");
}
}
綁定xaml:
<TabItem Header="Luminaire" Name="tabItem3" HorizontalAlignment="Center">
<TextBox TextWrapping="Wrap" FontFamily="Verdana" FontSize="13" Text="{Binding LuminRecevied,StringFormat=RX: {0}}" AcceptsReturn="True" TextChanged="TextBox_TextChanged_1" AcceptsTab="True" />
</TabItem>
另外,如果要附加,
this.serialData.LuminRecevied = sb.ToString() + Environment.NewLine;
將LuminRecevied設置為等於
sb.ToString() + Environment.NewLine;
您想這樣做
this.serialData.LuminRecevied += sb.ToString() + Environment.NewLine;
注意(+ =)
Change your code like this and try
<TabItem Header="Energy-Meter" Name="tabItem4">
<TextBox TextWrapping="Wrap" FontFamily="Verdana" FontSize="13"
Text="{Binding LuminRecevied ,StringFormat=RX: {0}}" AcceptsReturn="True"
TextChanged="TextBox_TextChanged_1" AcceptsTab="True" />
</TabItem>
在Text元素的綁定中將屬性更改為LuminRecevied應該就足夠了。 一個簡單的模型足以證明這一點:
public class ViewModel : INotifyPropertyChanged {
private string luminRecevied;
public string LuminRecevied
{
get { return luminRecevied; }
set
{
if (this.luminRecevied == value)
{
return;
}
this.luminRecevied = value;
this.InvokePropertyChanged("LuminRecevied");
}
}
private void InvokePropertyChanged(string propName)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl Height="311" HorizontalAlignment="Left" Name="tabControl1" VerticalAlignment="Top" Width="503">
<TabItem Header="Energy-Meter" Name="tabItem4">
<TextBox TextWrapping="Wrap" FontFamily="Verdana" FontSize="13" Text="{Binding LuminRecevied,StringFormat=RX: {0}}" AcceptsReturn="True" AcceptsTab="True" />
</TabItem>
</TabControl>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="93,0,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
聯播背后的代碼:
public partial class MainWindow : Window {
private ViewModel viewModel = new ViewModel();
public MainWindow()
{
InitializeComponent();
this.DataContext = viewModel;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.viewModel.LuminRecevied += "h";
}
}
this.serialData.LuminRecevied += String.Concat(
rawPacket.RawData.Select(i => i.ToString("X2")) + Environment.NewLine;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.