【问题标题】:Why is my combobox failing to bind to my List<string> in my ViewModel? WPF为什么我的组合框无法绑定到 ViewModel 中的 List<string>? WPF
【发布时间】: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,但没有成功。如果它确实有效,那将意味着我的模型类继承自我的基本视图模型,这是不对的,对吧?

标签: c# wpf combobox


【解决方案1】:

您当前绑定到的Conditions 属性不会返回IEnumerable,这是ItemsSource 属性所必需的。

您应该绑定到ConditionsModelConditions 列表属性:

<ComboBox Grid.Row="5" ItemsSource="{Binding Conditions.Conditions}" 
          SelectedItem="{Binding SelectedCondition}" />

【讨论】:

    【解决方案2】:

    您似乎绑定到 ConditionsModel 而不是 ConditionsModel.Conditions 属性。 除此之外,ObservableCollection 会在更新时触发NotifyPropertyChange 事件,而标准的List 不会。

    RelayCommand 触发,您的LoadInitialData(...) 函数将在创建并显示视图后调用。如果您在 ViewModel 构造函数中创建并填充了 Conditions,那么您可能会看到一些值。

    在这种情况下,最好使用ConditionsViewModel Conditions 而不是ConditionsModel Conditions 属性,其中ConditionsViewModel 使用ObservableCollection&lt;string&gt; Conditions 然后绑定到ConditionsViewModel.Conditions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 2014-01-31
      • 2017-10-18
      • 1970-01-01
      • 2011-12-03
      • 1970-01-01
      • 2011-10-14
      相关资源
      最近更新 更多