我正在尝试使用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>
答案 0 :(得分:2)
另外,如果你想要追加它,那么
this.serialData.LuminRecevied = sb.ToString() + Environment.NewLine;
将LuminRecevied设为等于
sb.ToString() + Environment.NewLine;
你想要改为
this.serialData.LuminRecevied += sb.ToString() + Environment.NewLine;
注意(+ =)
答案 1 :(得分:1)
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>
答案 2 :(得分:1)
在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";
}
}
答案 3 :(得分:1)
this.serialData.LuminRecevied += String.Concat(
rawPacket.RawData.Select(i => i.ToString("X2")) + Environment.NewLine;