-1

I have a textbox in WPF which looks like this:

<TextBox x:Name="EmployeeIdTextBox" TextWrapping="Wrap" IsEnabled="False" Margin="5,5,10,5" Grid.Row="0" Grid.Column="1" 
    Text="{Binding 
        ElementName=_selectedEmployee, 
        Path=Id, 
        Mode=TwoWay, 
        UpdateSourceTrigger=PropertyChanged}" />

And the binding source looks like this:

private Employee _selectedEmployee = Employee.GetEmployees();

I researched on the internet to find a solution to make the textbox update if object changes and vice versa, but my problem is that no change happens. What am i missing here?

  • how do you set `DataContext` for the Window? – ASh May 05 '16 at 10:57
  • 1
    I think you are missing a lot. _selectedEmployee must be a property, not a variable, you have to raise a property changed event after setting the property... – Pikoh May 05 '16 at 10:58
  • @Pikoh, not necessary. it might happen there is something like `EmployeeIdTextBox.DataContext = _selectedEmployee;` in code behind – ASh May 05 '16 at 11:00
  • 1
    @ASh,hmm..it could be, but that's something i think i would never do. Anyway, it's obvious there's not enough info to answer :) – Pikoh May 05 '16 at 11:01
  • @Pikoh yes i came across something like that. How exactly should the PropertyChanged event be raised? Can you please suggest me a good article if possible? – Yasith Jayawardana May 05 '16 at 11:06
  • Possible duplicate of [Two-way binding in WPF](http://stackoverflow.com/questions/320028/two-way-binding-in-wpf) – bit May 05 '16 at 11:08
  • @bit link would help you Yasith – Pikoh May 05 '16 at 11:09

1 Answers1

1

You should bind to public properties.

public Employee SelectedEmployee {get;set;}

...

SelectedEmployee = Employee.GetEmployees();
Paweł Zarzycki
  • 306
  • 3
  • 7