【发布时间】: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”值,而不是空字符串。
谁能解释我做错了什么以及我应该如何将默认的“可乐”值设置到组合框中?
【问题讨论】: