How to access WPF MainWindows Controls from another class in the same namespace?

13,887

Solution 1

One thing you can do is Create a constructor of SomeClass in which you want to access the listview and pass the reference of listview in constructor whenever you are creating the instance of SomeClass. In this way you will be able to access listview in any class

for example

in MainWindow.xaml.cs file

public MainWindow()
{
    InitializeComponent();
    SomeClass someClass = new SomeClass(listView);
}

in some other class where you want to access listview

public SomeClass
{
    ListView _ListViewRef;

    public SomeClass(ListView listView)
    {
    _LisViewRef = listView;
    }

   SomeMethod()
   {
   //here you can play with listview
   }

}

Solution 2

It is actually not a great idea to pass UI controls between classes.

You can only edit controls (such as listviews, textboxes etc..) in the class that controls the window.

You can, however, make another partial class that just splits your Main Window class.

If you ever pass something to another class, you can pass it this way:

Public SomeClass(string text)
{
}

//Create an object of 'SomeClass'
SomeClass someClass = new SomeClass(textBox.text);

Or you can pass the user control properties through methods.

Hope this helps,

Solution 3

In MainWindow.xaml you can create static instance of control you want to access;

 public static StackPanel stackPanel;
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // set stackpanel for logging
        stackPanel = stackPanel1;
    }

Then you can access it from outside class like;

MainWindow.stackPanel
Share:
13,887

Related videos on Youtube

Mariaczi
Author by

Mariaczi

Updated on June 04, 2022

Comments

  • Mariaczi
    Mariaczi almost 2 years

    I have MainWindows.cs like that:

    namespace LiniaProdukcyjna
    {
       /// <summary>
       /// Interaction logic for MainWindow.xaml
       /// </summary>
       public partial class MainWindow : Window
       {
          public MainWindow()
          {
             InitializeComponent();
          }
       }
    }
    

    And I have CSilnik class:

    namespace LiniaProdukcyjna
    {
        class CSilnik
        {
            ICollection<CLinia> linie;
    
            public void permut(int k, int n, int[] nums)
            {
                int i, j, tmp;
    
                /* when k > n we are done and should print */
                if (k <= n)
                {
    
                    for (i = k; i <= n; i++)
                    {
                        tmp = nums[i];
                        for (j = i; j > k; j--)
                        {
                            nums[j] = nums[j - 1];
                        }
                        nums[k] = tmp;
    
                        /* recurse on k+1 to n */
                        permut(k + 1, n, nums);
    
                        for (j = k; j < i; j++)
                        {
                            nums[j] = nums[j + 1];
                        }
                        nums[i] = tmp;
                    }
                }
                else
                {
                    linie.Add(new CLinia(nums));
                    // here i want to do something with ListView in MainWindow
                }
            }
        }
    }
    

    and CLinia class:

    namespace LiniaProdukcyjna
    {
        class CLinia
        {
            int koszt;
            int[] kolejnosc;
    
            public CLinia(int[] inKolejnosc)
            {
                kolejnosc = inKolejnosc;
            }
    
        }
    }
    

    And I have ListView control in MainWindow. I want to modify ListView "lista" in MainWindow, but I cannot access to them. What I have to do to accessing to controls like: lista.Items.Add ?