【问题标题】:WPF, Update ComboBox ItemsSource when it's DataContext changesWPF,当 DataContext 发生变化时更新 ComboBox ItemsSource
【发布时间】:2022-01-06 22:28:44
【问题描述】:

我有两个类 A 和 B,它们都实现了 IThingWithList 接口。

public interface IThingWithList
{
  ObservableCollection<int> TheList;
}

A 中的列表包含 1、2、3 B 中的列表包含 4、5、6

我有一个控制器类,它有一个包含 A 和 B 的 IThingWithList 列表

public class MyControllerClass
{
  public ObservableCollection<IThingWithList> Things { get; } = new ObservableCollection<IThingWithList>() { A, B };

  public IThingWithList SelectedThing { get; set; }
}

现在,在 xaml 中,我有两个组合框,如下所示

<ComboBox
  ItemsSource="{Binding MyController.Things}"
  SelectedValue="{Binding MyController.SelectedThing, Mode=TwoWay}" />

<ComboBox
  DataContext="{Binding MyController.SelectedThing}"
  ItemsSource="{Binding TheList}" />

第一个 ComboBox 控制哪个(A 或 B)是第二个组合框的数据上下文。

问题:

当我从第一个 ComboBox 中选择 A 或 B 时,第二个 ComboBox 的列表项没有更新。

我尝试过的:

使 A 和 B 都成为 ObservableObjects

使 IThingWithList 实现 INotifyPropertyChanged

将 UpdateSourceTrigger 添加到 ItemsSource 绑定

搜索谷歌。

【问题讨论】:

  • 您的绑定对象是否遵循 MVVM 模式?具体来说,您是否正在实施INotifyPropertyChanged?通常我只是在 ItemSources Bound 列表(应该是 ObservableCollection)的 Setter 中执行 OnPropertyChanged()。否则,您可能必须订阅 ObservableCollection。

标签: wpf combobox itemssource inotifycollectionchanged updatesourcetrigger


【解决方案1】:

这是我通常如何执行 ViewModel(在您的情况下为“控制器”)基类以获得您正在寻找的功能:

这是所有 VM 派生自的基类。

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged([CallerMemberName] string propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    protected void SetAndNotify<T>(ref T property, T value, [CallerMemberName] string propertyName = null)
    {
        if (Equals(property, value))return;

        property = value;
        this.OnPropertyChanged(propertyName);
    }

}

这是我将如何调整您的 ControllerClass:

public class MyControllerClass : ViewModelBase
{
    private ObservableCollection<IThingWithList> _things;
    public ObservableCollection<IThingWithList> Things
    {
        get => _things;
        set { SetAndNotify(ref _things, value); }
    }

    private IThingWithList _selectedthing;
    public IThingWithList SelectedThing
    {
        get => _selectedThing;
        set{SetAndNotify(ref _selectedThing, value);}
    }
}

现在调整您的 XAML 以设置容器的 DataContext 而不是每个控件(让生活更轻松)

<UserControl xmlns:local="clr-namespace:YourMainNamespace">
<UserControl.DataContext>
  <local:MyControllerClass/>
</UserControl.DataContext>
  <StackPanel>
    <ComboBox
      ItemsSource="{Binding Things}"
      SelectedValue="{Binding SelectedThing, Mode=TwoWay}" />

    <!-- ComboBox ItemsSource="See next lines" /-->
  </StackPanel>
</Window>

您可以将SelectedThing 更改为 ObservableCollection,或者您可以拥有第二个对象,即列表并相应地更新:

//Add into MyControllerClass

public MyInnerThingList => SelectedThing.TheList;

//Edit the SelectedThing to look like:
private IThingWithList _selectedthing;
public IThingWithList SelectedThing
{
    get => _selectedThing;
    set
    {
        SetAndNotify(ref _selectedThing, value);           
        RaisePropertyChanged(nameof(MyInnerThingList));
    }
}

然后将绑定更改为: &lt;ComboBox ItemsSource="{Binding MyInnerThingList, Mode=OneWay}" /&gt;

您可能还想添加 SelectedMyInnerThing 属性,但不确定是否需要。

【讨论】:

  • 谢谢!在我的例子中,重要的一点是控制器中 SelectedThing 上的 RaisePropertyChanged,回想起来应该很明显,但它被忽略了。
猜你喜欢
  • 1970-01-01
  • 2019-09-11
  • 1970-01-01
  • 2011-04-18
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2015-05-21
相关资源
最近更新 更多