tags:

views:

531

answers:

2

Hi,

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.

Cheers,

James

A: 

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

<TextBlock Text="{Binding StationIdInstruction}" />
Arcturus
+1  A: 

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}" />
Dmitri Nesteruk