简体   繁体   中英

C# appending data in textbox

I am trying to append data into textbox using MVVM. My problem is my data is not appending. Here is my code:

Model:

//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;

View model:

public string LuminRecevied {
    get { return luminRecevied; }
    set {
        if (this.luminRecevied == value) {
            return;
        }
        this.luminRecevied = value ;
        this.InvokePropertyChanged("LuminRecevied");
    }
}

Binding 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>

Also, if you want it to append, then

this.serialData.LuminRecevied = sb.ToString() + Environment.NewLine;

sets LuminRecevied equal to

sb.ToString() + Environment.NewLine;

You want to do this instead

this.serialData.LuminRecevied += sb.ToString() + Environment.NewLine;

Note the (+=)

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>

Changing the property to LuminRecevied in the binding for the Text element should be enough. A simple mock up is enough to prove this:

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>

Code behind hookup:

 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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