开发者

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 ListVie开发者_如何学运维w "lista" in MainWindow, but I cannot access to them. What I have to do to accessing to controls like: lista.Items.Add ?


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
   }

}


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,


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


Create Event Handler in Form2 and subscribe in Form1

public event EventHandler<UpdateListBoxEventArgs> UpdateListBox;     
      converter.UpdateListBox += 
 new EventHandler<CVEConverter.UpdateListBoxEventArgs>(AddToListBox);       
 public class UpdateListBoxEventArgs : EventArgs
    {
        private readonly string lbTerminalText;

        public UpdateListBoxEventArgs(string lbText)
        {
            this.lbTerminalText = lbText;
        }

        public string lbTerminalWindowText
        {
            get { return lbTerminalText; }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜