修改 ItemsSource ObservableCollection 后如何刷新组合框

发布于 2024-12-12 15:02:16 字数 1044 浏览 0 评论 0原文

问题很简单:更新 ItemsSource 时,组合框不会“刷新”,例如,新项目似乎没有添加到组合框中的项目列表中。

我已经尝试过此问题的接受答案中的解决方案:WPF - 自动刷新组合框内容 没有运气。

这是我的代码, XAML:

<ComboBox Name="LeadTypeComboBox" ItemsSource="{Binding LeadTypeCollection}" />

ViewModel:

public ObservableCollection<XmlNode> LeadTypeCollection { get; set; }

我更新此集合的方式是在单独的方法中,该方法从更新的 XML 文件加载数据: this.LeadTypeCollection = GetLeadTypesDataSource();

我也尝试过使用 Add< /code> 用于测试目的:

this.LeadTypeCollection = GetLeadTypesDataSource();
ItemToAdd = LeadTypeCollection[LeadTypeCollection.Count - 1];
this.LeadTypeCollection.Add(ItemToAdd);

代码更新集合肯定会开始,调试时我可以在该集合中看到新项目,但在组合框中看不到它们。

在 xaml 代码隐藏中执行此操作: LeadTypeComboBox.ItemsSource = MyViewModel.GetLeadTypesDataSource(); 但我想使用 MVVM 来实现此目的,即代码必须位于不知道的 ViewModel 中LeadTypeComboBox 控件的。

The problems is simple: when ItemsSource is updated Combobox doesn't "refresh" e.g. new items don't appear to be added to the list of items in the combobox.

I've tried the solution from aceepted answer to this question: WPF - Auto refresh combobox content with no luck.

here's my code,
XAML:

<ComboBox Name="LeadTypeComboBox" ItemsSource="{Binding LeadTypeCollection}" />

ViewModel:

public ObservableCollection<XmlNode> LeadTypeCollection { get; set; }

the way I update this collection is in the separate method, which loads data from updated XML file: this.LeadTypeCollection = GetLeadTypesDataSource();

I've also tried using Add for testing purposes:

this.LeadTypeCollection = GetLeadTypesDataSource();
ItemToAdd = LeadTypeCollection[LeadTypeCollection.Count - 1];
this.LeadTypeCollection.Add(ItemToAdd);

the code updating collection definitely kicks off, I can see new items in this collection when debugging, but I don't see them in the combobox.

Doing this in the xaml code-behind works: LeadTypeComboBox.ItemsSource = MyViewModel.GetLeadTypesDataSource(); but I'd like to achieve this with MVVM, i.e. the code must be in ViewModel which isn't aware of LeadTypeComboBox control.

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

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

发布评论

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

评论(3

情归归情 2024-12-19 15:02:18

Firedragons 的答案会起作用,但我更愿意只初始化一次 LeadTypeCollection 并使用清除、添加删除来更新您的集合。

var update = GetLeadTypesDataSource();     
this.LeadTypeCollection.Clear();

foreach(var item in update)
{
   this.LeadTypeCollection.Add(item);
}

如果数据上下文正确,您的 xaml 绑定应该可以工作

<ComboBox Name="LeadTypeComboBox" ItemsSource="{Binding LeadTypeCollection}" />

Firedragons answer would work, but i would prefer to initialize the LeadTypeCollection just once and use clear, add remove to update your collection.

var update = GetLeadTypesDataSource();     
this.LeadTypeCollection.Clear();

foreach(var item in update)
{
   this.LeadTypeCollection.Add(item);
}

your xaml binding should work if the datacontext is right

<ComboBox Name="LeadTypeComboBox" ItemsSource="{Binding LeadTypeCollection}" />
別甾虛僞 2024-12-19 15:02:18

我想我以前见过这种情况,解决方案是更新集合属性以引发更改。

public class MyViewModel : INotifyPropertyChanged
{
    private ObservableCollection<XmlNode> leadTypeCollection;

    public string LeadTypeCollection
    { 
        get { return leadTypeCollection; }
        set
        {
            if (value != leadTypeCollection)
            {
                leadTypeCollection = value;
                NotifyPropertyChanged("LeadTypeCollection");
            }
        }

    public MyViewModel()
    {
        leadTypeCollection = new ObservableCollection<XmlNode>();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        PropertyChanged.Raise(this, info);
    }
}

我有一个用于提升属性的扩展方法(如 stackoverflow 上其他地方所示):

public static void Raise(this PropertyChangedEventHandler handler, object sender, string propertyName)
{
    if (null != handler)
    {
        handler(sender, new PropertyChangedEventArgs(propertyName));
    }
}

I think I have seen this before and the solution was to update the collection property to raise the change.

i.e.

public class MyViewModel : INotifyPropertyChanged
{
    private ObservableCollection<XmlNode> leadTypeCollection;

    public string LeadTypeCollection
    { 
        get { return leadTypeCollection; }
        set
        {
            if (value != leadTypeCollection)
            {
                leadTypeCollection = value;
                NotifyPropertyChanged("LeadTypeCollection");
            }
        }

    public MyViewModel()
    {
        leadTypeCollection = new ObservableCollection<XmlNode>();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        PropertyChanged.Raise(this, info);
    }
}

I have an extension method for raising the property (as found elsewhere on stackoverflow):

public static void Raise(this PropertyChangedEventHandler handler, object sender, string propertyName)
{
    if (null != handler)
    {
        handler(sender, new PropertyChangedEventArgs(propertyName));
    }
}
筑梦 2024-12-19 15:02:18

一个简单的方法是将 ItemsSource 更改为空列表,然后将其更改回更新的源。我正在运行的项目的片段:

        RulesTable.ItemsSource = Rules.rulesEmpty;
        RulesTable.ItemsSource = Rules.Get();

A simple method is to change ItemsSource with empty list and then change it back to your updated source. A snippet from my project which is working:

        RulesTable.ItemsSource = Rules.rulesEmpty;
        RulesTable.ItemsSource = Rules.Get();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文