0

Is there another way to access MainWindow's public variables than :

MainWindow mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
mainWindow.variable....

this work fine, but I'm creating a WPF application and integrating a USB Webcam to my project and using this code above to access MainWindow's variables. This causes some problems like program is still running when I close MainWindow and camera won't stop. Any suggestions?

TestMan
  • 77
  • 1
  • 1
  • 15
  • Why are you trying to access a parent window public properties? If there is some data which needs to be accessible by two of the them, this data should really be located in other object, available to both, or passed to the child window by the parent. Take a look at the mvvm pattern. – igorc Jun 20 '17 at 12:13
  • I have settings panel in my MainWindow and couple of controls in settings panel (combobox and label). I'm trying to check which index of combobox is selected (in my SecondWindow) – TestMan Jun 20 '17 at 12:24
  • then create some class which will hold the data you need say "ControlData". in it put some properties and update them from the MainWindow. Pass this class to the child windows so he can access these properties – igorc Jun 20 '17 at 12:25

3 Answers3

0

This approach work also if you are developing a dll (you can't tell the same about Application.Current.MainWindow, which is clearer then your attempt anyways)

DependencyObject ucParent = this.Parent;
        while (!(ucParent is MainWindow))

        {
            ucParent = LogicalTreeHelper.GetParent(ucParent);
        }
        mainview = ucParent as MainWindow;

You can pack this inside a method, save the variables that you need and you shouldn't have problems

Daniele Sartori
  • 1,674
  • 22
  • 38
0

To make sure you application shuts down when MainWindow is closed:

public MainWindow()
    {
        InitializeComponent();
        this.Closing += (sender, args) => Application.Current.Shutdown();
    }
Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40
0

This is an over simplified example for what i wrote in comments(you really should look at mvvm the example below is not mvvm).

public class SelectedIndexData
{
    public int SelectedIndex { get; set; }
}

public partial class MainWindow : Window
{

    private readonly SelectedIndexData _selectedIndexData = new SelectedIndexData();
    public MainWindow()
    {
        InitializeComponent();
    }


    private void ComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        _selectedIndexData.SelectedIndex = ComboBox.SelectedIndex;
    }


    private void ShowChildWindow(object sender, RoutedEventArgs e)
    {
       new ChildWindow(_selectedIndexData).Show();
    }
}

public partial class ChildWindow : Window
{
    private SelectedIndexData _selectedIndexData;
    public ChildWindow(SelectedIndexData selectedIndexData)
    {
        InitializeComponent();
        _selectedIndexData = selectedIndexData; // do whatever you want with your data here

    }
}
igorc
  • 2,024
  • 2
  • 17
  • 29