2

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>
2
  • I can see only LuminRecevied where is your EnergyRecevied Property Commented Jul 2, 2015 at 9:22
  • Sorry! i posted the wrong part of the code. i edited it. its proper now. Commented Jul 2, 2015 at 9:41

4 Answers 4

2

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 (+=)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, still not able to append data.
Hi, when you say appending data, I'm just wondering if are you able to get anything to show up at all? I.e. when you call sb.ToString(), does the text box get populated by its value?
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>

1 Comment

Hi, i want my data to append in the textbox. but it simply overwrites
1

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";
    }
}

Comments

1
this.serialData.LuminRecevied += String.Concat(
   rawPacket.RawData.Select(i => i.ToString("X2")) + Environment.NewLine;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.