I have a class with properties for an employee
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace SimpleDatabinding03
{
public class Employee:INotifyPropertyChanged
{
int _employeenumber;
string _firstname;
string _lastname;
string _dept;
string _title;
//constructor
public Employee()
{
}
//properties
public int EmployeeNum
{
get { return _employeenumber; }
set { _employeenumber = value; NotifyPropertyChanged("EmployeeNum"); }
}
public string FirstName
{
get { return _firstname; }
set { _firstname = value; NotifyPropertyChanged("FirstName"); }
}
public string LastName
{
get { return _lastname; }
set { _lastname = value; NotifyPropertyChanged("LastName"); }
}
public string Dept
{
get { return _dept; }
set { _dept = value; NotifyPropertyChanged("Dept"); }
}
public string Title
{
get { return _title; }
set { _title = value; NotifyPropertyChanged("Title"); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyname)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
//internal object GetBindingExpression(System.Windows.DependencyProperty dependencyProperty)
//{
// throw new NotImplementedException();
//}
}
}
and in the XAML I have bound them to a textbox:
<Grid>
<Grid.DataContext>
<m:Employee x:Name="employee"/>
</Grid.DataContext>
<Label Grid.Row="0">Employee Number</Label>
<TextBox Name="EmpNum" Grid.Row="0" Grid.Column="1" Text="{Binding EmployeeNum}" ></TextBox>
<Label Grid.Row="2" >first name</Label>
<TextBox Name="Fname" Grid.Row="2" Grid.Column="1" Text="{Binding FirstName}"></TextBox>
<Label Grid.Row="3" >Last name</Label>
<TextBox Name="Lname" Grid.Row="3" Grid.Column="1" Text="{Binding LastName}"></TextBox>
<Label Grid.Row="4" >Dept</Label>
<TextBox Name="Dept" Grid.Row="4" Grid.Column="1" Text="{Binding Dept}"></TextBox>
</Grid>
the code behind is:
private void button1_Click(object sender, RoutedEventArgs e)
{
employee.EmployeeNum = 123;
System.Threading.Thread.Sleep(3000);
employee.FirstName = "John";
System.Threading.Thread.Sleep(3000);
employee.LastName = "kepler"; });
}
Requirement: When the property changes the UI textbox which is bound to the employee instance is not updated. it waits for the button click event to complete and then the UI is updated. I'm looking for a solution where the UI is updated on the fly.
Windows UIs in .net are typically single-threaded. You're blocking the UI thread inside of the method. You can use the following to more cleanly handle this:
Note the use of async, await and Task.Delay. For more information on these, see Asynchronous Programming with Async and Await (C# and Visual Basic)
Edit - .net 3.5 answer:
Since you're on .net 3.5, that complicates things slightly. You generally do not want to update objects bound to the UI from any thread but the UI thread, but, you can do your hard work from another thread, then pass through to the UI thread.
The key thing to note here is that the Dispatcher will make sure that the actions invoked will happen on the UI thread, while the 'work' will happen on the background thread.