简体   繁体   English

使用MVVM在组合框中设置默认值

[英]Set default value in a combo box using MVVM

I'm trying to set a default value into a combo box when the application is first loading using the MVVM pattern and it looks like this is all the time unset, combo box being all the time empty when the page loads. 我正在尝试在使用MVVM模式首次加载应用程序时将默认值设置为组合框,并且看起来这一直是未设置的,组合框在页面加载时始终为空。

This is my xaml: 这是我的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}" />

This is the view model code, with its default constructor: 这是视图模型代码,具有默认构造函数:

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

where I am trying to set the default value of the combo box. 我试图设置组合框的默认值。

And this is how the properties looks like: 这就是属性的样子:

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");
        }
    }

When selecting an item from combo box the selection is updated into the model and also into the view, so it is working as expected but when the page is first loaded even if the "SelectedComboBoxOptionIndex" and "SelectedItemOption" are being called and their value updated the view of the page is not updated and the empty string is being shown into the combo box where I was expected to see the "Cola" value, instead of the empty string. 从组合框中选择一个项目时,选择会更新到模型中,也会更新到视图中,因此它按预期工作,但是当首次加载页面时,即使调用“SelectedComboBoxOptionIndex”和“SelectedItemOption”并更新其值页面视图未更新,空字符串显示在组合框中,我希望看到“Cola”值,而不是空字符串。

Can someone explain me what I am doing wrong and how should I set the default "Cola" value into the combo box ? 有人可以解释一下我做错了什么,如何将默认的“Cola”值设置到组合框中?

Only bind the SelectedItem property of the ComboBox to the SelectedItemOption source property and set the latter to the string "Cola" in the view model. 仅将ComboBoxSelectedItem属性绑定到SelectedItemOption源属性,并将后者设置为视图模型中的字符串“Cola”。 This should work: 这应该工作:

<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";
}

Don't mix SelectedItem , SelectedIndex and SelectedValue . 不要混合SelectedItemSelectedIndexSelectedValue You only need one. 你只需要一个。

The value of juiceOperations.SelectedItemOption , that is, "Cola" , is not the same "Cola" stored in the ItemsSource . juiceOperations.SelectedItemOption的值,即"Cola" ,与ItemsSource存储的"Cola" You would need to do something like juiceOperations.SelectedItemOption = juiceOperations.JuiceOptions.First() . 您需要执行juiceOperations.SelectedItemOption = juiceOperations.JuiceOptions.First()

mm8 above absolutely right, that should fix your issue. mm8以上绝对正确,这应该可以解决您的问题。

On a side note, what you have there will work for a static selection list, but consider using an ObservableCollection<string> instead of a List<string> . 另外,您所拥有的内容适用于静态选择列表,但请考虑使用ObservableCollection<string>而不是List<string> The former implements INotifyCollectionChanged, which allows the view to be notified if there has been a change in the collection. 前者实现了INotifyCollectionChanged,它允许在集合中发生更改时通知视图。 When you bind an Observable Collection to the view, the view automatically subscribes to the CollectionChanged event. 将Observable Collection绑定到视图时,视图会自动订阅CollectionChanged事件。 You will need this if you ever need to add or remove options at run time. 如果您需要在运行时添加或删除选项,则需要此选项。 Side note, OnCollectionChanged will not fire if you simply modify an item, for that you would still need to call OnPropertyChanged("JuiceOptions") in the setter. 旁注,如果您只修改一个项目,OnCollectionChanged将不会触发,因为您仍然需要在setter中调用OnPropertyChanged("JuiceOptions")

something like this (with the appropriate private backing field): 这样的事情(有适当的私人支持领域):

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

    set
    {
        this.juiceOptions = value;
        this.OnPropertyChanged("JuiceOptions");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM