【问题标题】:How to access WPF MainWindows Controls from another class in the same namespace?如何从同一命名空间中的另一个类访问 WPF MainWindow 控件?
【发布时间】:2011-05-24 17:16:09
【问题描述】:

我有这样的 MainWindows.cs:

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

我有 CSilnik 课:

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
            }
        }
    }
}

和 Clinia 类:

namespace LiniaProdukcyjna
{
    class CLinia
    {
        int koszt;
        int[] kolejnosc;

        public CLinia(int[] inKolejnosc)
        {
            kolejnosc = inKolejnosc;
        }

    }
}

我在 MainWindow 中有 ListView 控件。 我想在 MainWindow 中修改 ListView “lista”,但我无法访问它们。 我必须做些什么才能访问以下控件:lista.Items.Add ?

【问题讨论】:

  • 你能告诉我们ListView的定义吗?如果它是 MainWindow.xaml 中定义的控件,您只需添加一个 Name 然后您可以通过 MyListView.Items.Add() 在 MainWindow.xaml.cs 中访问它跨度>

标签: c# wpf visual-studio-2010


【解决方案1】:

您可以做的一件事是创建SomeClass 的构造函数,您希望在其中访问listview,并在创建SomeClass 的实例时在constructor 中传递listview 的引用。这样你就可以在任意class中访问listview

例如

在 MainWindow.xaml.cs 文件中

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

在您想要访问列表视图的其他类中

public SomeClass
{
    ListView _ListViewRef;

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

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

}

【讨论】:

    【解决方案2】:

    在类之间传递 UI 控件实际上并不是一个好主意。

    您只能在控制窗口的类中编辑控件(如列表视图、文本框等)。

    但是,您可以创建另一个仅拆分主窗口类的部分类。

    如果你曾经将某些东西传递给另一个类,你可以这样传递它:

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

    或者你可以通过方法传递用户控件的属性。

    希望这会有所帮助,

    【讨论】:

      【解决方案3】:

      在 MainWindow.xaml 中,您可以创建要访问的控件的静态实例;

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

      然后你可以从外部类访问它;

      MainWindow.stackPanel
      

      【讨论】:

        【解决方案4】:

        在Form2中创建Event Handler并在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; }
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-04-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-23
          • 2019-06-09
          • 1970-01-01
          • 2012-06-23
          • 1970-01-01
          相关资源
          最近更新 更多