【问题标题】:Set default value in a combo box using MVVM使用 MVVM 在组合框中设置默认值
【发布时间】:2017-10-08 18:04:38
【问题描述】:

我正在尝试在应用程序首次使用 MVVM 模式加载时将默认值设置为组合框,看起来这一直是未设置的,当页面加载时组合框一直为空。

这是我的 xaml:

    <ComboBox Grid.Row="0" Margin="10,0,0,0" Grid.Column="1" 
              SelectedItem="{Binding Path=JuiceOperations.SelectedItemOption, Mode=TwoWay}"
              SelectedIndex="{Binding Path=JuiceOperations.SelectedComboBoxOptionIndex, Mode=TwoWay}"
              SelectedValue="{Binding Path=JuiceOperations.SelectedComboBoxOptionIndex, Mode=TwoWay}"
              ItemsSource="{Binding Path=JuiceOperations.JuiceOptions}" />

这是视图模型代码,带有它的默认构造函数:

    public JuiceViewModel()
    {
        juiceOperations.SelectedComboBoxOptionIndex = 0;
        juiceOperations.SelectedItemOption = "Cola";
    }

我正在尝试设置组合框的默认值。

这就是属性的样子:

private List<string> juiceOptions = new List<string> { "Cola", "Sprite", "Fanta", "Pepsi" };

    private string selectedItemOption = string.Empty;

    private int selectedComboBoxOptionIndex = 0;

    public int SelectedComboBoxOptionIndex
    {
        get
        {
            return this.selectedComboBoxOptionIndex;
        }

        set
        {
            this.selectedComboBoxOptionIndex = value;
            this.OnPropertyChanged("SelectedComboBoxOptionIndex");
        }
    }

    public List<string> JuiceOptions
    {
        get
        {
            return this.juiceOptions;
        }

        set
        {
            this.juiceOptions = value;
        }
    }

    public string SelectedItemOption
    {
        get
        {
            return this.selectedItemOption;
        }

        set
        {
            this.selectedItemOption = value;
            this.OnPropertyChanged("SelectedItemOption");
        }
    }

当从组合框中选择一个项目时,选择会更新到模型和视图中,因此它按预期工作,但是当页面首次加载时,即使正在调用“SelectedComboBoxOptionIndex”和“SelectedItemOption”并且它们的值已更新,页面视图未更新,空字符串显示在组合框中,我希望在该组合框中看到“Cola”值,而不是空字符串。

谁能解释我做错了什么以及我应该如何将默认的“可乐”值设置到组合框中?

【问题讨论】:

    标签: c# wpf mvvm combobox


    【解决方案1】:

    仅将ComboBoxSelectedItem 属性绑定到SelectedItemOption 源属性,并将后者设置为视图模型中的字符串“Cola”。这应该有效:

    <ComboBox Grid.Row="0" Margin="10,0,0,0" Grid.Column="1" 
              SelectedItem="{Binding Path=JuiceOperations.SelectedItemOption}"
              ItemsSource="{Binding Path=JuiceOperations.JuiceOptions}" />
    

    public JuiceViewModel()
    {
        juiceOperations.SelectedItemOption = "Cola";
    }
    

    不要混用 SelectedItemSelectedIndexSelectedValue。你只需要一个。

    【讨论】:

    • 另外,我认为您需要使 ItemSource Mode = TowWay,以便用户界面能够显示从 ViewModel 完成的设置
    【解决方案2】:

    mm8 以上绝对正确,应该可以解决您的问题。

    附带说明,您所拥有的内容适用于静态选择列表,但请考虑使用ObservableCollection&lt;string&gt; 而不是List&lt;string&gt;。前者实现了 INotifyCollectionChanged,它允许在集合发生更改时通知视图。当您将 Observable Collection 绑定到视图时,视图会自动订阅 CollectionChanged 事件。如果您需要在运行时添加或删除选项,您将需要它。旁注,如果您只是修改一个项目,OnCollectionChanged 将不会触发,因为您仍然需要在 setter 中调用OnPropertyChanged("JuiceOptions")

    类似这样的东西(带有适当的私有支持字段):

    public ObservableCollection<string> JuiceOptions
    {
        get
        {
            return this.juiceOptions;
        }
    
        set
        {
            this.juiceOptions = value;
            this.OnPropertyChanged("JuiceOptions");
        }
    }
    

    【讨论】:

      【解决方案3】:

      juiceOperations.SelectedItemOption的值,即"Cola",与ItemsSource中存储的"Cola"不同。你需要做类似juiceOperations.SelectedItemOption = juiceOperations.JuiceOptions.First()的事情。

      【讨论】:

      • 嗨,我应该去哪里?进入视图模型?
      • 自然的地方。我可能会在模型的构造函数中定义所有 JuiceOptionsSelectedItemOptionSelectedComboBoxOptionIndex,但您也可以在视图模型中完成,就像您已经完成的那样。
      • 好的,我将此代码添加到视图模型中,并且属性正在按预期更新,但在加载应用程序时组合框的值仍然为空。
      • 另一件有点奇怪的事情是SelectedValue 的使用。在使用SelectedItem 时,您不需要它,并且在您的XAML 中,您将值设置为0,这不是ItemsSource 中任何项目的值。也许从 XAML 中删除 SelectedValue 完全有帮助? (同样,我发现在几乎所有情况下,当您拥有 SelectedItem 时,SelectedIndex 也是多余的,但是否适合您取决于应用程序。)
      • 我只是试图使用这三个选项中的任何一个(SelectedIndex、SelectedValue 或 SelectedItem)来设置该默认值,但即使我只使用其中一个或所有这些选项都不起作用.
      猜你喜欢
      • 2014-02-14
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-08
      • 2017-10-03
      相关资源
      最近更新 更多