WPF 组合框 在属性更改事件时未更新

我的 WPF 应用程序中有一个自定义组合框,并且在初始启动后更新我的项源时,组合框未更新。itemsSource 是一个自定义的可观察词典。我已经在我的属性上实现了INotifyPropertyChanged与我的可观察词典,但它不会更新。我已经搜索了每个WPF属性更改事件,该事件在堆栈溢出上不起作用,我正在寻找一些帮助。


自定义组合框代码:


<controls:MultiSelectComboBox Width="100" Height="30" Padding="0 10 0 0" Grid.Row="0" Grid.Column="1"

    ItemsSource="{Binding Mode=TwoWay,             

                  UpdateSourceTrigger=PropertyChanged,                                                    

                  ValidatesOnDataErrors=True, 

                  Path=DataContext.OptionTeamMembersDictionary, 

                  BindsDirectlyToSource=True,                                                    

                  RelativeSource={RelativeSource AncestorType={x:Type UserControl},Mode=FindAncestor}}"

    SelectedItems="{Binding SelectedOptionTeamMembersDictionary, Mode=TwoWay}"

    x:Name="TeamMemberDisplay" 

    ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}"/>

属性和私有变量:


private ObservableDictionary<string, object> _optionTeamMembersDictionary;

private ObservableDictionary<string, object> _selectedOptionTeamMembersDictionary;



public ObservableDictionary<string, object> OptionTeamMembersDictionary

{

    get

    {

        return _optionTeamMembersDictionary;

    }

    set

    {

        _optionTeamMembersDictionary = value;

        OnPropertyChanged("OptionTeamMembersDictionary");

    }

}


public ObservableDictionary<string, object> SelectedOptionTeamMembersDictionary

{

    get

    {

        return _selectedOptionTeamMembersDictionary;

    }

    set

    {

        _selectedOptionTeamMembersDictionary = value;

        OnPropertyChanged("SelectedOptionTeamMembersDictionary");

    }

}

我使用一个按钮来触发数据库拉取,该拉取将每行引导到一个对象中,然后在返回多行数据时填充选项TeamMemberDictionary。


当数据加载到我的构造函数中时,上述所有内容都有效,但是当它在启动后加载时,我的组合框不会显示集合中的新数据。


牧羊人nacy
浏览 159回答 1
1回答

三国纷争

经过多次故障排除,我能够找出答案。onItemsSourceChanged 在实例化控件后未触发,因此在应用程序初始启动后从未进行过更新。我将多功能组合盒上的OnItemsSourceChanged函数编辑到下面,以便它可以在更改的事件上触发,并且它现在按预期工作。private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MultiSelectComboBox control = (MultiSelectComboBox)d;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var action = new NotifyCollectionChangedEventHandler(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (o, args) =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MultiSelectComboBox c = (MultiSelectComboBox)d;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c?.DisplayInControl();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (e.OldValue != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sourceCollection = (ObservableDictionary<string, object>)e.OldValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sourceCollection.CollectionChanged -= action;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (e.NewValue != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sourceCollection = (ObservableDictionary<string, object>)e.NewValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sourceCollection.CollectionChanged += action;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; control.DisplayInControl();&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP