Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 285
Mariaczi Avatar asked Nov 20 '25 08:11

Mariaczi


2 Answers

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
   }

}
like image 66
Haris Hasan Avatar answered Nov 21 '25 21:11

Haris Hasan


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,

like image 34
Jordan Carroll Avatar answered Nov 21 '25 21:11

Jordan Carroll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!