【发布时间】:2023-03-15 17:07:01
【问题描述】:
我的 XAML 中有四个组合框。它看起来像这样:
<ComboBox Grid.Row="2" ItemsSource="{Binding VehiclesAvailable}" DisplayMemberPath="FleetNumber"/>
<ComboBox Grid.Row="3" ItemsSource="{Binding Employees_VehicleEligible}" DisplayMemberPath="FullName"/>
<ComboBox Grid.Row="4" ItemsSource="{Binding Jobsites}" DisplayMemberPath="Name"/>
<ComboBox Grid.Row="5" ItemsSource="{Binding Conditions}" SelectedItem="{Binding SelectedCondition}" />
前三个 CB 绑定到 ObservableCollection 属性,这些都可以正常工作。 最后一个绑定到一个List,它不显示任何内容。
在 VM 中,我声明属性并在 LoadInitialData 命令中设置其值。
//PROPERTIES
public ObservableCollection<VehicleModel> VehiclesAvailable { get; set; }
public ObservableCollection<EmployeeModel> Employees_VehicleEligible { get; set; }
public ObservableCollection<JobsiteModel> Jobsites { get; set; }
public ConditionsModel Conditions { get; set; }
public GenericCondition SelectedConditionOut { get; set; }
public string SelectedCondition { get; set; }
//COMMANDs
public RelayCommand<object> LoadIntialDataCommand { get; set; }
//METHODS
public async void LoadInitialData(object e)
{
var vehiclesAvailable = await GetData.VehiclesAvailableQueryAsync();
foreach (VehicleModel _vehicle in vehiclesAvailable)
VehiclesAvailable.Add(_vehicle);
var employees_VehiclesEligible = await GetData.Employees_VehiclesEligibleQueryAsync();
foreach (EmployeeModel _employee in employees_VehiclesEligible)
Employees_VehicleEligible.Add(_employee);
Conditions = new ConditionsModel();
}
对于它的价值,这是我定义条件模型的地方:
class ConditionsModel
{
private List<string> _conditions;
public List<string> Conditions
{
get { return new List<string>() { "New", "Excellent", "Good", "Fair", "Poor", "Unacceptable" }; }
set { _conditions = value; }
}
当我单步执行代码时,在 LoadInitialData 方法的右括号处放置一个断点,Conditions 似乎填充了所有字符串。当我转到 XAML 时,果然,当我将鼠标悬停在 ItemsSource 属性上时,所有字符串都会出现。但是当我运行它时,组合框是空的。调试此问题的下一步可能是什么?
【问题讨论】:
-
在设置
Conditions时可以尝试调用 OnPropertyChanged("Conditions") 吗? => 设置 { _conditions = 值; OnPropertyChanged("条件"); } -
感谢您的参与,Raya,但没有成功。如果它确实有效,那将意味着我的模型类继承自我的基本视图模型,这是不对的,对吧?