组合框项目源和所选项目同步

发布于 2024-11-23 22:07:03 字数 602 浏览 2 评论 0原文

我有一个绑定到 ViewModel 中的 ObservableCollection 的组合框。当用户添加新项目时,我不知道他们是否已将项目添加到绑定集合中,因此我进行刷新。好吧,这将所选项目设置为空,因此我实现了一些代码来记住所选项目并在更新集合后设置它。问题是集合不显示所选项目。

if (_selectedBrand != null)
{
    int selectedBrandID = _selectedBrand.BrandID;
    Brands = null;
    Brands = new ObservableCollection<Brand>(_dataContext.Brands.ToList());
    SelectedBrand = _dataContext.Brands.First<Brand>(b => b.BrandID == selectedBrandID);
}

如何让集合显示更正后的项目?

编辑:该集合代表后端数据库中的一个表。用户可以打开一个新窗口将项目添加到数据库中。窗口关闭后,我必须刷新集合才能获取新项目。抱歉我的措辞混乱。

I have a combobox that is bound to ObservableCollection<Item> in the ViewModel. When a user adds a new item I don't know if they've added an item to the bound collection so I do a refresh. Well this sets the selecteditem to null so I implemented some code to remember the selected item and set it after the Collection is renewed. The problem is the collection doesn't display the selecteditem.

if (_selectedBrand != null)
{
    int selectedBrandID = _selectedBrand.BrandID;
    Brands = null;
    Brands = new ObservableCollection<Brand>(_dataContext.Brands.ToList());
    SelectedBrand = _dataContext.Brands.First<Brand>(b => b.BrandID == selectedBrandID);
}

How can I get the collection to display the corrected item?

Edit: The collection represents a table in the backend DB. The user can open a new window to add items to the DB. Once the window is closed I must refresh the collection to get any new items. Sorry for the confusion of my wording.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

潦草背影 2024-11-30 22:07:03

您能否不只是检索新集合,然后更新现有集合并添加缺失值(使用 linq 扩展除外?)

Can you not just retrieve the new collection, and then update the existing one adding in the missing values (using the linq extension except?)

笔落惊风雨 2024-11-30 22:07:03

您将 SelectedBrand 设置为 _dataContext.Brands 中的一个项目,该项目在 Brands ObservableCollection 中不存在。

更改代码以将 SelectedBrand 设置为 ObservableCollection 中的项目,而不是 _dataContext

SelectedBrand = Brands.First<Brand>(b => b.BrandID == selectedBrandID);

You are setting your SelectedBrand to an item in your _dataContext.Brands, which doesn't exist in the Brands ObservableCollection.

Change your code to set SelectedBrand to an item in your ObservableCollection, not your _dataContext.

SelectedBrand = Brands.First<Brand>(b => b.BrandID == selectedBrandID);
柠北森屋 2024-11-30 22:07:03

我假设这段代码位于按钮单击事件处理程序的上下文中?所以我不确定为什么你每次都必须重新初始化集合。您应该能够仅使用 Brands.Add 方法。我的工作场所没有 .net 3.5,所以我只需要对其进行伪编码。

我不知道这是否是您正在寻找的内容,但也许它会有所帮助。

public sealed partial class ObservableCollectionExample : Form
{

private ObservableCollectionExample()
{
     InitializeComponent();
}

private ObservableCollectionExample_Load(object sender, EventArgs e)
{
      //Subscribe to the collection changed event
      Brands.CollectionChanged += new EventArgs<NotifyCollectionChangedEventHandler>(Brands_Changed);
}

private Brands_Changed(object sender, EventArgs e)
{
      //Add code to do something when the Brands collection is changed i.e. something is added or removed via the .Add or .Remove method
}

private void Button_Click(object sender, EventArgs e)
{
     if (_selectedBrand != null)
     {
          Brands.Add(foobar);
          if (_dataContext.Brands.First<Brand>(b => b.BrandID == selectedBrandID)) // Do some checking to see if it got added to the collection
          {
          SelectedBrand = _selectedBrand.BrandId;
          }
     }
}
}

I'm assuming this code is in the context of a Button Click eventhandler? So I'm not sure why you are havign to reinitialize the collection everytime. You should be able to just use the Brands.Add method. I don't have .net 3.5 at my workplace so I'll just have to psuedo code it.

I don't know if this is what you are looking for but maybe it can help.

public sealed partial class ObservableCollectionExample : Form
{

private ObservableCollectionExample()
{
     InitializeComponent();
}

private ObservableCollectionExample_Load(object sender, EventArgs e)
{
      //Subscribe to the collection changed event
      Brands.CollectionChanged += new EventArgs<NotifyCollectionChangedEventHandler>(Brands_Changed);
}

private Brands_Changed(object sender, EventArgs e)
{
      //Add code to do something when the Brands collection is changed i.e. something is added or removed via the .Add or .Remove method
}

private void Button_Click(object sender, EventArgs e)
{
     if (_selectedBrand != null)
     {
          Brands.Add(foobar);
          if (_dataContext.Brands.First<Brand>(b => b.BrandID == selectedBrandID)) // Do some checking to see if it got added to the collection
          {
          SelectedBrand = _selectedBrand.BrandId;
          }
     }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文