Reputation: 13
I have created this base class for INotifyPropertyChanged...
namespace BASECLASSES.HelperClasses
{
public class NotifyPropChangedBase : INotifyPropertyChanged
{
/// <summary>
/// The propertyChanged Event to raise to any UI object
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// The PropertyChanged Event to raise to any UI object
/// The event is only invoked if data binding is used
/// </summary>
/// <param name="propertyName"></param>
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
PropertyChangedEventArgs args = new PropertyChangedEventArgs(propertyName);
handler(this, args);
}
}
}
}
I have created a ViewModel which implements the base class here...
public class CheckoutVM : BASECLASSES.HelperClasses.NotifyPropChangedBase
{
private string fullName ;
public string FullName
{
get { return fullName; }
set
{
if (fullName != value)
{
fullName = value;
RaisePropertyChanged("FullName");
}
}
}
}
}
}
In XAML I have defined a namespace for the class...
xmlns:bcbcns="clr-Namespace:BASECLASSES.HelperClasses;assembly=BASECLASSES"
I have defined a window resource...
<Window.Resources>
<m:CheckoutVM x:Key="chkOutViewModel" />
</Window.Resources>
Set the DataContext to the main grid...
<Grid DataContext="{Binding Source={StaticResource chkOutViewModel}}">
Set the path for label content to...
<Label Name="txtContactCheckingOut" Content="{Binding Path=FullName}"/>
Next I set the label with this code...
List<GET_CONTACT_Result> contactResultList = modsqlContact.GET_CONTACT(contactID);
CheckoutVM checkOutContact = new CheckoutVM();
checkOutContact.FullName = contactResultList[0].cFULLNAME;
But the label is not set.
If I add a constructor to the ViewModel like this....
public CheckoutVM()
{
FullName = "XXXXXXXXXXXXXXXXX";
}
The label is set to XXXXXXXXXXXXXXXXX so the binding seems to be correct.
It is looking like the handler is always null. PLEASE HELP!!!! What am I doing wrong???
Upvotes: 0
Views: 46
Reputation: 5313
Your resources are available in the code behind through the Resources
property, which is the merged dictionary of resources. Your item will be available through the key that you've given it. Assuming that your example code for setting the FullName
property is in your Window code behind, the following code should allow you to update the value to the bound instance.
List<GET_CONTACT_Result> contactResultList = modsqlContact.GET_CONTACT(contactID);
var contact = (CheckoutVM)Resources["chkOutViewModel"];
contact.FullName = contactResultList[0].cFULLNAME;
Upvotes: 1
Reputation: 1389
The problem it that your checkOutContact is not the same instance of CheckoutVM as the one used in your control.
One way of solving this is to set the view model in your Window's code-behind. Something like this:
public CheckoutVM ViewModel
{
get { return (CheckoutVM) DataContext; }
set { DataContext = value; }
}
Then you remove the DataContext from your Grid. By default, the controls in your Window will take the same DataContext as your Window.
<Grid>
...
<Label Name="txtContactCheckingOut" Content="{Binding FullName}"/>
...
</Grid>
And you initialize your window like this:
YourWindow yourWindow = new YourWindow();
yourWindow.ViewModel = new CheckoutVM();
yourWindow.ViewModel.FullName = contactResultList[0].cFULLNAME;
This should do the trick.
Upvotes: 1