
Untitled
By: a guest on
Aug 11th, 2012 | syntax:
None | size: 1.42 KB | hits: 4 | expires: Never
Binding System uses Reflection to find Properties in DataContext then What is the use of Implementing Interface and IOC/DI in ViewModel
namespace ViewModel
{
public class MyViewModel : IMyViewModel, INotifyPropertyChanged
{
public MyViewModel()
{
FirstName = "Harish";
LastName = "Chauhan";
}
private string _firstName;
private string _lastName;
public string FirstName
{
get
{
return _firstName;
}
set
{
_firstName = value;
Notify("FirstName");
}
}
public string LastName
{
get
{
return _lastName;
}
set
{
_lastName = value;
Notify("LastName");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private void Notify(string propName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
//Interface
public interface IMyViewModel
{
string FirstName { get; set; }
}
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="70"/>
<RowDefinition Height="70"/>
</Grid.RowDefinitions>
<TextBox x:Name="tbx1" Text="{Binding FirstName, Mode=TwoWay}"/>
<TextBox x:Name="tbx2" Text="{Binding LastName, Mode=TwoWay}" Grid.Row="1"/>
</Grid>