属性更改通知绑定到静态字典?

时间:2014-10-17 14:13:25

标签: c# wpf mvvm dictionary combobox

围绕这个问题圆了。

我有2个ComboBoxes。用户在第一个ComboBox&第二个ComboBox根据第一个选择显示选项。

我将第二个ComboBox绑定到我的视图模型中公开的静态Dictionary<string,string>。我有另一个公共属性,它在静态字典上引发了INPC。在UI中,不显示更改。我在“输出”窗口中没有出现任何错误。有什么我想念的吗?

P.S。这在.NET4.0下运行良好。一旦用户计算机安装了.NET4.5,此行为仅在此1 ComboBox绑定到Dictionary ...

时启动

属性:

    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" />

1 个答案:

答案 0 :(得分:0)

好的,感谢@Mike Strobel,问题是由于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");
        }
    }