0

Been round in circles with this issue.

I have 2 ComboBoxes. User makes a selection on the first ComboBox & the 2nd ComboBox displays options depending on the 1st selection.

I'm binding the 2nd ComboBox to a static Dictionary<string,string> exposed in my view model. I have another public property which raises INPC on the static Dictionary. In the UI, the changes are not displayed. I'm getting no errors in the Output window. Is there something I'm missing?

p.s. this functioned fine under .NET4.0. As soon as user machines installed .NET4.5, this behaviour started only with this 1 ComboBox bound to a Dictionary...

Properties:

    private static Dictionary<string, string> _ModelArticleTypeCodeToChangeTitleMap;
    public static Dictionary<string, string> ModelArticleTypeCodeToChangeTitleMap
    {
        get { return _ModelArticleTypeCodeToChangeTitleMap; }
        set 
        { 
            _ModelArticleTypeCodeToChangeTitleMap = value;               
        }
    }

    //Default ArticleTypeCodeToTitleMapFilteredByCategory dictionary to the full list (as no Category will have been initially selected)
    private Dictionary<string, string> _ModelArticleTypeCodeToTitleMapFilteredByCategory = ModelArticleTypeCodeToTitleMap;
    public Dictionary<string, string> ModelArticleTypeCodeToTitleMapFilteredByCategory
    {
        get { return _ModelArticleTypeCodeToChangeTitleMap; }
        set
        {
            _ModelArticleTypeCodeToChangeTitleMap = value;
            OnPropertyChanged("ModelArticleTypeCodeToChangeTitleMap");
        }
    }

Xaml:

<ComboBox DisplayMemberPath="Value" HorizontalAlignment="Left", Converter={StaticResource invertBoolConverter}}" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Path=ModelArticleCategoryToTitleMap, Mode=TwoWay}" SelectedValue="{Binding ModelSelectedArticleCategory}" SelectedValuePath="Key"/>
<ComboBox ItemsSource="{Binding Path=ModelArticleTypeCodeToChangeTitleMap, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsEditModeTitleReadOnly, Converter={StaticResource invertBoolConverter}}" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding ModelSelectedArticleTypeCode}" Text="{Binding ModelEnteredTitle}" TabIndex="1" />
Hardgraf
  • 2,566
  • 4
  • 44
  • 77
  • Try raising a static event called `ModelArticleTypeCodeToChangeTitleMapChanged` instead of using `INotifyPropertyChanged`. – Mike Strobel Oct 17 '14 at 14:41
  • Found this http://stackoverflow.com/questions/11473256/twoway-binding-of-a-combobox-to-a-static-property-in-net-4-5 , seems like it may be a bug? I've installed 4.5.2 but still no luck... – Hardgraf Oct 17 '14 at 14:42
  • Workaround: Move it to a singleton. Add that singleton as a Resource in app.xaml, and you can use the StaticResource binding to access it. –  Oct 17 '14 at 14:48
  • looks like this link is helpful to you http://msdn.microsoft.com/en-us/library/bb613588%28v=vs.110%29.aspx#static_properties – King King Oct 17 '14 at 15:03
  • @Will, do you have an example of exposing 1 singleton property in the ViewModel? Obviously I don't want the whole ViewModel as a Singleton class & the property needs to live in the Viewmodel. Cheers – Hardgraf Oct 21 '14 at 08:59
  • What I would normally do is add a VM that contains the property, then add that VM as a Resource `` then use a static resource binding to access `Text="{Bidning Herp, Source={StaticResource derp}}"` –  Oct 21 '14 at 12:55

1 Answers1

0

Ok, thanks @Mike Strobel 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");
        }
    }
Hardgraf
  • 2,566
  • 4
  • 44
  • 77