【问题标题】:How to trigger reload of combobox itemsource如何触发组合框项目源的重新加载
【发布时间】:2018-03-23 11:17:39
【问题描述】:

我有一个组合框,我正在将 XAML 中的组合框与 viewmodel 字典值绑定。

第一次加载页面时,我尝试从服务器下载字典值并将其设置为字典视图模型变量。

但是组合框看起来是空的,我不明白为什么会发生这种情况,因为 View Model 变量已经更新,这应该会触发组合框的重新加载,但这并没有发生..

仅供参考: 如果我硬编码字典而不是从服务器下载它,我看不到这个问题 当我第二次加载页面时,我没有看到这个问题

更新

XAML

<ComboBox x:Name=“testBox” Margin=“0,0,0,0” PlaceholderText="{StaticResource testText}” ItemsSource="{Binding TestDictionary.Values}” SelectedValue="{Binding DictionaryValue, Mode=TwoWay}" IsEnabled="{Binding IsItLoading, Converter={StaticResource InverseBooleanConverter}}"/>

查看模型

       private Dictionary<string, string> testDictionary;


 public Dictionary<string, string> TestDictionary
        {
            get
            {
                if (this.testDictionary == null)
                {
                    this.testDictionary = new Dictionary<string, string>();
                }

                return this.testDictionary;
            }

            set
            {
                this.Set(() => this.TestDictionary, ref this.testDictionary, value);
            }
        }

【问题讨论】:

  • 您能否发布您的ViewModel 和XAML 您如何将数据分配给Combobox?
  • @AVK - 我已经更新了有问题的代码示例

标签: xaml combobox mvvm-light uwp-xaml xamarin.uwp


【解决方案1】:

Dictionary 不会在添加、删除项目或刷新整个列表时提供通知。

当我们向Dictionary 添加新数据时,我们应该能够将null 设置为ComboBox 的ItemsSource。然后将ComboBox的TestDictionary.Values设置为ItemsSource。

您也可以实现自己的ObservableDictionary。当Dictionary 更改时,ComboBox 将更改。 要实现ObservableDictionary,可以参考following question。

【讨论】:

    猜你喜欢
    • 2012-07-02
    • 2019-04-21
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多