I have a base abstract viewmodel containing 2 static models. In a usercontrol, I bind to both. Only the one binding (language) resolves and updates when the underlying static model changes. The binding to Employee does not update the fields. I have tested that the Employee setter is called and indeed it is. The getter however does not get called after the value changes. Any guidance will be appreciated.
Base Viewmodel:
namespace POC.Windows
{
public abstract class ViewModel : ObservableObject, IDataErrorInfo
{
private static int _languageId;
private static Languages _language;
private static Employee _employee;
public Employee Employee
{
get { return _employee; }
set
{
_employee = value;
NotifyPropertyChanged();
}
}
public int LanguageId
{
get{return _languageId;}
set{
if (_languageId != value)
{
_languageId = value;
LoadLanguage(); //Async populate Language model
NotifyPropertyChanged();
}
}
}
public Languages Language
{
get
{
if (_language == null)
{
_language = new Languages();
_languageId = -1;
LoadLanguage();
}
return _language;
}
set
{
_language = value;
NotifyPropertyChanged();
}
}
//Async Loading functions here - omitted
}
}
UserControl View:
<UserControl x:Class="POC.DesktopClient.UserControls.EmployeeDetails"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModels="clr-namespace:POC.DesktopClient.ViewModels"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance viewModels:EmployeeDetailsViewModel}">
<UserControl.Resources>
<viewModels:EmployeeDetailsViewModel x:Key="EmployeeDetailsViewModel"/>
</UserControl.Resources>
<Grid>
<StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="0">
<TextBlock VerticalAlignment="Top" Margin="10"
Text="{Binding Path=Language.FirstName}"
Foreground="DarkBlue" FontWeight="Bold" FontSize="20">
</TextBlock>
</StackPanel>
<StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="1">
<TextBlock VerticalAlignment="Top" Margin="10"
Text="{Binding Path=Employee.FirstName}"
Foreground="DarkBlue" FontWeight="Bold" FontSize="20">
</TextBlock>
</StackPanel>
</Grid>
haven't touched the usercontrol's xaml.cs and EmployeeDetailsViewModel is empty:
public class EmployeeDetailsViewModel : ViewModel
{
}
---EDIT--- Observable Object:
namespace POC.Windows
{
public class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Additional information:
- The Usercontrol is hosted in the MainWindow.xaml.
- In MainWindow.xaml.cs a datacontext is defined as a MainViewModel (that also is blank and inherits from ViewModel).
- The static Employee model is bound to a listbox's selectedItem property.
- The listbox is in a separate usercontrol also on the MainWindow.
- Ths listbox's viewmodel is also blank and inherits from the base viewmodel. Its xaml.cs is blank, datacontext is set in Xaml, same as the details usercontrol.
- LanguageID is bound to a combobox selectedValue on the MainWindow.
So in a nutshell, when I change the language in the combobox, the usercontrol's labels bound to language updates correctly. When I select an employee in the listbox, usercontrol's labels bound to Employee remains blank.