[英].NET4.5 Binding a Combobox to a Dictionary Issues. WPF
I'm maintaining an internal WPF app which has a ComboBox bound to a Dictionary property in the ViewModel. 我正在维护一个内部WPF应用程序,该应用程序具有绑定到ViewModel中的Dictionary属性的ComboBox。 The app was written in VS2010 targeting .NET4.0 and everything worked fine.
该应用程序是针对NET2010的VS2010编写的,一切正常。
User machines are now auto updating to .NET4.5 & this particular ComboBox is not displaying the values in the UI at runtime. 用户计算机现在自动更新到.NET4.5,并且此特定的ComboBox在运行时未在UI中显示值。 This is the only binding to a Dictionary in the app.
这是应用程序中唯一绑定到Dictionary的绑定。 I've scoured the Output Window in VS2013 but there's no apparent binding errors etc. There is a method which populates the dictionary & all is populated correctly & I've implemented INPC.
我已经在VS2013中对“输出窗口”进行了搜索,但是没有明显的绑定错误等。有一种方法可以填充字典,所有方法均可以正确填充,并且我已经实现了INPC。 Is there some difference in the way .NET4.5 binds to dictionaries?
.NET4.5绑定到字典的方式是否有所不同?
Xaml: 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 Property: ViewModel属性:
private Dictionary<string, string> _ModelArticleTypeCodeToTitleMapFilteredByCategory = ModelArticleTypeCodeToTitleMap;
public Dictionary<string, string> ModelArticleTypeCodeToTitleMapFilteredByCategory
{
get { return _ModelArticleTypeCodeToChangeTitleMap; }
set
{
_ModelArticleTypeCodeToChangeTitleMap = value;
OnPropertyChanged("ModelArticleTypeCodeToChangeTitleMap");
}
}
Ok, the issue was due to the implementation of INotifyPropertyChanged not working with a static property. 好的,此问题是由于INotifyPropertyChanged的实现不适用于静态属性。 I wrote a static version of the PropertyChanged event and raised the event in the property setter:
我编写了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));
}
}
Property: 属性:
private Dictionary<string, string> _ModelArticleTypeCodeToTitleMapFilteredByCategory = ModelArticleTypeCodeToTitleMap;
public Dictionary<string, string> ModelArticleTypeCodeToTitleMapFilteredByCategory
{
get { return _ModelArticleTypeCodeToChangeTitleMap; }
set
{
_ModelArticleTypeCodeToChangeTitleMap = value;
NotifyStaticPropertyChanged("ModelArticleTypeCodeToChangeTitleMap");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.