【问题标题】:WPF Combobox binding selected itemWPF Combobox 绑定选中项
【发布时间】:2020-10-10 11:25:51
【问题描述】:

我有以下问题。我有这个模型:

public class A
{
    public A(string id)
    {
        ID = id;
        
    }

    public string ID { get; }
    public string Name { get ; set; }
    public B objectB { get; set; }

}

public class B
{
    public B(string id)
    {
        ID = id;
    }

    public string ID { get; }
    public string Name { get; set; }
}

public class ViewModel
{
    public ObservableCollection<A> ListOne = new ObservableCollection<A>();
    public ObservableCollection<B> ListTwo = new ObservableCollection<B>();

    public ViewModel()
    {
        B objectB = new B("1");
        objectB.Name = "ObjectB";

        B listElement2 = new B("2");
        listElement2.Name = "ListElement2";

        ListTwo.Add(objectB);
        ListTwo.Add(listElement2);

        A objectA = new A("A1");
        objectA.Name = "objectA1";
        objectA.objectB = objectB;

        ListOne.Add(objectA);
    }
}

<ListBox ItemsSource="{Binding ListOne}" Name="MyListBox">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <Grid>
        <ComboBox ItemsSource="{Binding ElementName=MyListBox, Path=DataContext.ListTwo}" 
                  DisplayMemberPath="Name" 
                  SelectedItem="{Binding objectB}"/>
      </Grid>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

Class A 包含一个对象 ob Class B。 我的ViewModel 包含两个列表。 A 类对象的一个​​列表 (ListOne) 和第二个列表 (ListTwo) 包含 B 类的对象。

现在我想将listTwo 绑定到嵌入到listbox 中的combobox。 listbox 显示视图模型的listOne 中的所有对象。 如果我更改combobox 的选择,我想更新列表框所选项目的属性objectB。我还希望combobox 在初始化期间选择objectB 的值。

请注意:我简化了模型和视图模型以专注于问题。 NotifyProperties 等工作。我对组合框的绑定感兴趣。

我不知道这样做。我希望你能帮助我。

【问题讨论】:

  • 你能发布你的 xaml 吗?
  • 我添加了 xaml
  • 当然还有 viewModel。

标签: c# wpf xaml combobox listbox


【解决方案1】:

您可以执行以下操作:

<ComboBox
    ItemsSource="{Binding ListB}"
    SelectedItem="{Binding SelectedB}"
    DisplayMemberPath="Name" Width="100"
/>

另外,您可以在 ViewModel 中添加以下代码:

private ObservableCollection<B> listB = new ObservableCollection<B>();
public ObservableCollection<B> ListB
{
    get { return this.listB; }
    set { this.listB = value; NotifyPropertyChanged("ListB"); }
}

private B selectedB;
public B SelectedB
{
    get { return this.selectedB; }
    set { this.selectedB = value; NotifyPropertyChanged("SelectedB"); }
}
ViewModel()
{
    //Fill your ListB with all possible values
    this.SelectedB=ListB.FirstOrDefault(x=>x.ID==A.ObjectB.ID)
}

那么你的类可能是 INotifyPropertyChanged :

public class A : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
    public long ID=-1;
    private string name = "";
    public string Name
    {
        get { return this.name; }
        set
        {
            this.name = value;
            this.NotifyPropertyChanged("Name");
        }
    }
    private B objectB;
    public B ObjectB
    {
        get{return this.objectB;}
        set
        {
            this.objectB=value;
            this.NotifyPropertyChanged("B");
        }
    }

}

我几乎可以肯定您也可以将 SelectedB 替换为 A.ObjectB 进行绑定,但需要进行一些测试来检查它。优点(或不方便取决于)是,如果您在 A.ObjectB 上绑定,则更改将在您更改 ComboBox 中的选定项目后立即生效。这样您就可以更改您的 ComboBox 值而无需直接修改您的 A 对象,然后在关闭窗口时,您可以询问是否保存,如果是,只需执行 A.ObjectB=SelectedB。

【讨论】:

  • 嗨 Siegfried.V 感谢您的帖子。但是我的 Combobox 的 ItemsSource 不是 ListOne。 ListOne 在每个列表元素的属性“objectB”中包含我要在组合框中选择的项目,该组合框中以 ListTwo 作为源。所以属性“objectB”是我在两个列表之间的桥梁。
  • 啊我明白了,没注意到对不起,我现在编辑答案
  • 其实问题不仅出在XAML,而且你的类也不正确,姐姐我明白你的A类里面有一个对象的列表吗?
  • 那我应该如何设计我的课程呢?我想要实现的是:我有一个 A 型对象,它在内部拥有一个 B 型对象(Property ObjectB)。在视图中,我显示类型 A 的对象,在组合框中,我选择了类型 B 的对象。组合框的选定元素希望将其存储在属性 ObjectB 中。当我再次在视图中调用类型 A 对象时,我希望在组合框中选择我在属性 ObjectB 中保存的类型 B 对象。使用 www.DeepL.com/Translator 翻译(免费版)
  • @mannmawg 所以在你的视图中,你只显示一个对象 A,然后 ComboBox 对应于一个对象列表,对吧?对不起,我在工作中耽搁了,没有看到时间飞逝......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 2011-11-01
  • 2015-04-07
  • 1970-01-01
相关资源
最近更新 更多