简体   繁体   中英

.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. The app was written in VS2010 targeting .NET4.0 and everything worked fine.

User machines are now auto updating to .NET4.5 & this particular ComboBox is not displaying the values in the UI at runtime. This is the only binding to a Dictionary in the app. 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. Is there some difference in the way .NET4.5 binds to dictionaries?

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:

    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. I wrote a static version of the PropertyChanged event and raised the event in the property setter:

// 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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM