.NET4.5将组合框绑定到字典问题.WPF
本文关键字:字典 问题 WPF 绑定 组合 NET4 | 更新日期: 2023-09-27 18:26:39
我正在维护一个内部WPF应用程序,该应用程序有一个绑定到ViewModel中Dictionary属性的ComboBox。该应用程序是在VS2010中针对.NET4.0编写的,一切都很好。
用户机器现在自动更新到.NET4.5&这个特定的组合框在运行时不会在UI中显示值。这是应用程序中唯一绑定到字典的绑定。我在VS2013中搜索了Output Window,但没有明显的绑定错误等;全部正确填充&我已经实现了INPC。NET4.5绑定到字典的方式有什么不同吗?
Xaml:
<ComboBox ItemsSource="{Binding Path=ModelArticleTypeCodeToChangeTitleMap,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Converter={StaticResource invertBoolConverter}}"
IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedValue="{Binding ModelSelectedArticleTypeCode}"
Text="{Binding ModelEnteredTitle}" />
ViewModel属性:
private Dictionary<string, string> _ModelArticleTypeCodeToTitleMapFilteredByCategory = ModelArticleTypeCodeToTitleMap;
public Dictionary<string, string> ModelArticleTypeCodeToTitleMapFilteredByCategory
{
get { return _ModelArticleTypeCodeToChangeTitleMap; }
set
{
_ModelArticleTypeCodeToChangeTitleMap = value;
OnPropertyChanged("ModelArticleTypeCodeToChangeTitleMap");
}
}
好的,问题是由于INotifyPropertyChanged的实现无法使用静态属性。我编写了PropertyChanged事件的静态版本,并在属性设置器中引发了该事件:
// INotifyPropertyChanged event for static properties!
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
private static void NotifyStaticPropertyChanged(string propertyName)
{
if (StaticPropertyChanged != null)
{
StaticPropertyChanged(null, new PropertyChangedEventArgs(propertyName));
}
}
属性:
private Dictionary<string, string> _ModelArticleTypeCodeToTitleMapFilteredByCategory = ModelArticleTypeCodeToTitleMap;
public Dictionary<string, string> ModelArticleTypeCodeToTitleMapFilteredByCategory
{
get { return _ModelArticleTypeCodeToChangeTitleMap; }
set
{
_ModelArticleTypeCodeToChangeTitleMap = value;
NotifyStaticPropertyChanged("ModelArticleTypeCodeToChangeTitleMap");
}
}