0

I have the follow situation:

One textbox that updates when string value changes, like a log field.

my implementation:

XAML

...

       <TextBox x:Name="txtLogView" Margin="10" Grid.Row="1" TextWrapping="Wrap">
            <TextBox.Text>
                <Binding Path="TextoLog"  Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
            </TextBox.Text>
        </TextBox> 

...

SerialManager.cs

...
private StringBuilder logText;
...

public String TextoLog
    {
        get { return logText.ToString(); }
        set { logText.Append(value); OnPropertyChanged("TextoLog"); }

    }

...

  private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {

        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        TextoLog = indata;

     }

The variable logText is updated when a new data arrives on serial port, i saw that in "set" property of Textlog, put the "get" property never is called and the textbox never shows the text.

2
  • Are you anywhere setting the DataContext of the Window (or any other parent of the TextBox control) to a SerialManager instance? Otherwise the Binding would not have a source object. Commented Aug 12, 2015 at 13:00
  • On mainwindow i'm using the follow: this.DataContext = this; Commented Aug 12, 2015 at 13:05

1 Answer 1

0

The error was resolved setting the Datacontext of textbox on mainwindow initialization, as follow:

  private void InitDataModels()
        {
            SerialManager = new SerialManager();
            cbPorts.DataContext = SerialManager;
            txtLogView.DataContext = SerialManager;

            this.DataContext = this;
        }
Sign up to request clarification or add additional context in comments.

1 Comment

If SerialManager is a property of your Window class, you may avoid to set the TextBox DataContext explicitly by changing the Path of the Binding: <Binding Path="SerialManager.TextoLog" ...>.

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.