Access WPF Name properties in static method

c#, wpf

Solution

`this` is not accessible in static method. You can try save reference to your instance in static property, for example:

public class MyWindow : Window
{

    public static MyWindow Instance { get; private set;}

    public MyWindow() 
    {
        InitializeComponent();
        // save value
        Instance = this; 
    }

    public static getControl()
    {
        // use value
        if (Instance != null)
            var control = Instance.switchcontrol; 
    }

    protected override void OnClosed(EventArgs e)
    {
        base.OnClosed(e);
        Instance = null; // remove reference, so GC could collect it, but you need to be sure there is only one instance!!
    }

}

Problem

I have a WPF application. In one of the XAML I have used Name attribute like as follows ``` x:Name="switchcontrol" ``` I have to access the control/property in .cs file using `this.switchcontrol` My question is, I need to access the control in static method like ``` public static getControl() { var control = this.switchcontrol;//some thing like that } ``` How to achieve this?

Original source