将字符串数组添加到ObservableCollection< string>不行 [英] Add string array to ObservableCollection<string> does not work

查看:323
本文介绍了将字符串数组添加到ObservableCollection< string>不行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将字符串数组中的字符串添加到ObservableCollection。



我的方式是:



在我的构造函数中,我实例化了集合,并添加了eventhandler来调用我的方法,将数组中的字符串添加到列表中:

 code> public CLoggerViewModel()
{
this._currenLogContent = new ObservableCollection< string>();
this._memoryAppender.LogContentChanged + = new LoggingEventHandler(OnLogContentChanged);
}

在事件处理程序中,我想迭代字符串数组,并将每个字符串添加到收集:

  public void OnLogContentChanged(object sender,LoggingEventArgs e)
{
string [] tmpString = {A,B,C,D};

foreach(tmpString中的字符串)
{
_currenLogContent.Add(s);
}
}

在我的论证中

  e.LogEntries 

我的字符串数组我一直都是我期望的所有字符串。
为了简单/测试,我不使用我从我的参数得到的数组,并将其与A,B,C,D实例化,因为我被要求以这种方式进行更改。 >

我的问题是,始终只是数组的第一个字符串将被添加到我的ObservableCollection。
添加第一个字符串后,似乎我的foreach迭代终止了。



编辑:



在我的WPF XAML文件中,如果我的ObservableCollection绑定到以下列表框:

 < ListBox Name =listViewLog ItemsSource ={Binding LoggerViewModel.CurrentLogContent,UpdateSourceTrigger = PropertyChanged}Margin =0,0,-248,-377Grid.ColumnSpan =2RenderTransformOrigin =0.586,0.51Height =367VerticalAlignment =底部/> 

如果我使用绑定到我的Observablecollection的列表框,它会遍历foreachloop。因此,列表框或从列表框到我的集合的绑定导致停止迭代。我已经尝试使用所有UpdateSourceTrigger选项(Default,Explicit,LostFocus,PropertyChanged)。
但是在所有情况下,它会在第一轮之后停止迭代。



还有其他任何我必须在ListBox属性中设置以避免停止添加字符串到Observablecollection?



编辑:



我发现适用于我的唯一解决方案是将我想在单独的类中添加到集合中并将对象添加到列表中的字符串。在这种情况下,如果我将对象添加到列表中,它不会破坏我的foreach循环。

  public void OnLogContentChanged(object sender,LoggingEventArgs e)
{
string [] tmpString = {A,B,C,D};

foreach(tmpString中的字符串)
{
this.LogEntries.Add(new CLogEntry(s));
}

}

CLogEntry只是一个公开的Wrapper一个单一的字符串。



有了这个解决方法,它的工作原理,但仍然,我不明白为什么它不起作用,如果我直接添加一个字符串到Observablecollection。 p>

解决方案

您是否将另一个数据绑定设置为您的ObservableCollection?还是设置控件来控制数据绑定到你的ListBox?因为你的代码似乎是正确的,我无法重现这个问题,我认为有一个数据有界的逻辑由ObservableCollection触发的案例一个错误 - 这个错误防止迭代完成。


I try to add strings that are in a string array to an ObservableCollection.

The way i do it is:

In my constructor i instantiate the collection and add the eventhandler to call my method that adds the strings from the array to the list:

 public CLoggerViewModel()
    {
        this._currenLogContent = new ObservableCollection<string>();
        this._memoryAppender.LogContentChanged += new LoggingEventHandler(OnLogContentChanged);
    }

In the eventhandler i want to iterate through the string array and add each string to the collection:

 public void OnLogContentChanged(object sender, LoggingEventArgs e)
 {
    string[] tmpString = { "A", "B", "C", "D" };

    foreach (string s in tmpString)
    {
        _currenLogContent.Add(s);
    }
 }

In my Argument

e.LogEntries

which holds my string array i have always all the strings that i expect. For simplicity/test i dont use the array that i get from my argument and instantiate it with A,B,C,D , since i was asked to change it in way that it can be verified here.

My problem is that always just the first string of the array will be added to my ObservableCollection. Once the first string is added it seems that my foreach iteration gets terminated.

EDIT:

In my WPF XAML file if have the following list box where my ObservableCollection is bound to:

<ListBox Name="listViewLog" ItemsSource="{Binding LoggerViewModel.CurrentLogContent,UpdateSourceTrigger=PropertyChanged}" Margin="0,0,-248,-377" Grid.ColumnSpan="2" RenderTransformOrigin="0.586,0.51" Height="367" VerticalAlignment="Bottom" />

If i remove the listbox with the binding to my Observablecollection it iterates through the foreachloop. So the listbox or the binding from the listbox to my collection is causing the stop of the iteration. I already tried to use all the UpdateSourceTrigger options (Default,Explicit,LostFocus,PropertyChanged). But in all cases it stops the iteration after the first round.

Is there anything else that i have to set in the ListBox properties to avoid stopping to add strings to the Observablecollection?

EDIT:

The only solution i found that works for me is to Wrap the strings that i want to add to the collection in a separate class and adding the objects to the list. In this case it does not break my foreach loop if i add objects to the list.

public void OnLogContentChanged(object sender, LoggingEventArgs e)
    {
        string[] tmpString = { "A", "B", "C", "D" };

        foreach (string s in tmpString)
        {
            this.LogEntries.Add(new CLogEntry(s));
        }

    }

CLogEntry is just a Wrapper that exposes a single string.

With this workaround it works, but still, i cant understand why it does not work if i "directly" add a string to the Observablecollection.

解决方案

Have you set up another Data Binding to your ObservableCollection? Or have you set up Control to Control Data Binding to your ListBox? As your code seems to be right and I can not reproduce the problem, I think there is a data bounded "logic" triggered by the ObservableCollection cases an error - this error prevent the iteration to be completed.

这篇关于将字符串数组添加到ObservableCollection&lt; string&gt;不行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆