Display a collection from another class

  • Thread starter Thread starter DenisAndreevich
  • Start date Start date
D

DenisAndreevich

Guest
Hi everyone.

There is a WPF application and a collection exists in one of the classes ObservableCollection.

In the current view, so that the ListBox displays the contents of the collection implements the following process:

public

public someClass()
{
ObservableCollection<SomeType> messages = new ObservableCollection<SomeType>();

private async void GetMessagesUpdate()
{
// Many strings of code
messages.Add(new SomeStruct (string1, string2, string3));
// Many strings of code
}

public ObservableCollection<SomeType> SomeMethod()
{
return messages
}
}

And in the MainWindow


ObservableCollection<SomeType> messages = new ObservableCollection<SomeType>();
commandViewer.ItemsSource = messages;

private void Button_Click(object sender, RoutedEventArgs e)
{
ObservableCollection<SomeType> temp = SomeClass.messages();
foreach (var item in temp)
{
messages.Clear();
messages.Add(new SomeStruct(string1, string2, string2));
}
}

I am sure that there is an easier way to transfer collections between classes without creating intermediate collections.
Is it possible to implement the code in such a way that ListBox would pick up a collection change from another class?

Continue reading...
 
Back
Top