我有一个包含2个静态模型的基本抽象viewmodel。在usercontrol中,我绑定到两者。当底层静态模型发生更改时,只有一个绑定(语言)会解析并更新。与Employee的绑定不会更新字段。我已经测试了Employee setter被调用,实际上是。然而,在值更改后不会调用getter。任何指导将不胜感激。
基本视图模型:
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查看:
<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>
未触及usercontrol的xaml.cs且EmployeeDetailsViewModel为空:
public class EmployeeDetailsViewModel : ViewModel
{
}
--- --- EDIT 可观察对象:
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));
}
}
}
简而言之,当我更改组合框中的语言时,usercontrol的标签绑定到语言正确更新。当我在列表框中选择一个员工时,绑定到Employee的usercontrol标签仍为空白。