如何从同一命名空间中的另一个类访问 WPF MainWindows 控件?

发布于 2024-11-09 03:47:44 字数 1758 浏览 0 评论 0原文

我有这样的 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 之类的控件?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天气好吗我好吗 2024-11-16 03:47:44

您可以做的一件事是创建 SomeClass 的构造函数,在其中您想要访问 listview 并在 中传递 listview 的引用每当您创建 SomeClass 实例时,都会使用构造函数。通过这种方式,您将能够在任何 class 中访问 listview

例如

MainWindow.xaml.cs 文件中

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

在您想要访问 listview 的其他类的

public SomeClass
{
    ListView _ListViewRef;

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

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

}

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
   }

}
请远离我 2024-11-16 03:47:44

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

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

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

如果您将某些内容传递给另一个类,您可以通过这种方式传递它:

Public SomeClass(string text)
{
}

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

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

希望这有帮助,

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,

高冷爸爸 2024-11-16 03:47:44

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

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

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

MainWindow.stackPanel

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
‖放下 2024-11-16 03:47:44

在 Form2 中创建事件处理程序并在 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; }
        }
    }

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; }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文