2

I have a question about data binding which I am struggling with.

I have the following property in my xaml.cs file:

    private string _stationIdInstruction;

    public event PropertyChangedEventHandler PropertyChanged;

    public string StationIdInstruction
    {
        get { return _stationIdInstruction; }
        set
        {
            _stationIdInstruction = value;
            OnPropertyChanged("StationIdInstruction");
        }
    }

    protected void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

How can I bind a TextBlock to StationIdInstructions so it picks up the string property as its Text and update the TextBlock.Text when I update StationIdInstructions.

Any help is appreciated.

1
  • Your source code has a small error: either you forgot an opening "{" after "if (PropertyChanged != null)", or you're closing a non-existing if-block. Commented Apr 14, 2011 at 13:29

2 Answers 2

4

Yes, and don't forget to specify the binding context. E.g.,

<Window ... Name="MyWindow">
  <Grid DataContext="{Binding ElementName=MyWindow, Path=.}">
    <TextBlock Text="{Binding Path=StationIdInstruction}" />
Sign up to request clarification or add additional context in comments.

Comments

-1

Set your StationIdInstructions object on the DataContext of your control, and your TextBlock like so:

<TextBlock Text="{Binding StationIdInstruction}" />

1 Comment

Doesn't work. Error says a dependency property is missing.

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.