C
C#7mo ago
! 0Falco

How to refer to the mainwindow class from another class.

Creating a WPF app. I have one main class where all the mainwindow controls are operated.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeAPIs();
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeAPIs();
}
I have a different class that's called FilterManager. In this class I want to operate some data on the mainwindow as well. But I'm not sure how, what's a clean way to refer to the mainwindow from this class?
internal class FilterManager
{
// Reference to MainWindow here
}
internal class FilterManager
{
// Reference to MainWindow here
}
3 Replies
! 0Falco
! 0FalcoOP7mo ago
I tried the following, suggested by ChatGPT
private MainWindow mainWindow;

public FilterManager(MainWindow mainWindow)
{
this.mainWindow = mainWindow;
}
private MainWindow mainWindow;

public FilterManager(MainWindow mainWindow)
{
this.mainWindow = mainWindow;
}
But I still cannot access the listview in my WPF UI like this
weatherListView.Items.Clear();
weatherListView.ItemsSource = FilteredWeatherData;
weatherListView.Items.Clear();
weatherListView.ItemsSource = FilteredWeatherData;
SpReeD
SpReeD7mo ago
You should use the MVVM pattern in which you access the ViewModel and set the Properties accordingly. The View shouldn't initialize any API or any business logic at all. Other than that, your ChatGPT code is fine, the reference of the MainWindow is set through the constructor and can be accessed throughout the Class like so this.mainWindow.weatherListView.Items.Clear();
! 0Falco
! 0FalcoOP7mo ago
ah yes true, that would be a lot cleaner thanks I'll go ahead and do that
Want results from more Discord servers?
Add your server