Add array of strings to ObservableCollection <string> not working

I am trying to add rows that are in an array of strings to an ObservableCollection.

How do i do this:

In my constructor, I instantiate the collection and add an event handler to call my method, which adds the rows from the array to the list:

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

In the event handler, I want to iterate over an array of strings and add each row 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 contains my string array, I always have all the strings that I expect. For simplicity / test, I do not use the array that I get from my argument, and create it with A, B, C, D, as I was asked to change it so that it can be checked here.

My problem is that always only the first line of the array will be added to my ObservableCollection. After adding the first line, it seems like my foreach iteration is ending.

EDIT:

In my WPF XAML file, if I have the following list in which my ObservableCollection is bound:

 <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 delete the list bound to my Observablecollection, it will iterate through foreachloop. Thus, a list or binding from a list to my collection causes the iteration to stop. I already tried to use all parameters of UpdateSourceTrigger (Default, Explicit, LostFocus, PropertyChanged). But in all cases, it stops the iteration after the first round.

Is there anything else I should set in the ListBox properties to avoid stopping adding rows to the Observablecollection?

EDIT:

The only solution I found for me is to wrap the strings I want to add to the collection in a separate class and add 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 provides a single line.

In this workaround, this works, but still I can’t understand why it doesn’t work if I “directly” add a line to the Observablecollection.

0
source share
1 answer

Have you set another data binding to your ObservableCollection? Or did you configure Control to control data binding to your ListBox? Since your code seems to be correct and I cannot reproduce the problem, I think there is a data-limited “logic” caused by ObservableCollection cases, an error - this error prevents the iteration from completing.

0
source

Source: https://habr.com/ru/post/975016/


All Articles