Link to home
Start Free TrialLog in
Avatar of bickes0724
bickes0724Flag for United States of America

asked on

WPF, C# - Is it Possible to Set the ItemsSource of a Control in a Different Class From A Static Method?

Is it Possible to Set the ItemsSource of a Control in a Different Class From A Static Method?

I have an addin (class 1) that allows you to view and filter table records. Below the filters is a ListView control that displays saved queries and a button to open a window that manages those queries. In the window (class 2) you can add, edit, and delete queries. After the user saves changes, I would like to update the ListView control in class 1 from class 2. If possible I would like to use the following syntax:


//Class 1 Static Method:
public static void SetListViewItemsSource(ObservableCollection<Query> queries)
{
      this.listViewControl.ItemsSource = queries;
}

// To Change ItemsSource From Class2:
Class1.SetListViewItemsSource(queryCollection);

Open in new window


I know this approach is not possible because you cannot use the "this" keyword in a static method. Is there an alternative, WITHOUT having to create and instance of Class1??

Thanks!
Avatar of dimaj
dimaj
Flag of United States of America image

This might be a dumb question...
How are you planning to see the control if you are not instantiating Class1?

As far as an approach goes, take a look at events. You could pass data via EventArgs when a query changes.
Avatar of bickes0724

ASKER

Class 1 is already instantiated because it is loaded first. The popup window loads from a button clicked on the Class 1. So, when an update to the user saves changes, in Class 2, I just want to copy the changes/collection to the ItemsSource in Class 1.

The event approach sounds promising, I am not positive how to implement it.
ASKER CERTIFIED SOLUTION
Avatar of bickes0724
bickes0724
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, did the events approach work?
Thanks