如何从同一命名空间中的另一个类访问 WPF MainWindows 控件?
我有这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以做的一件事是创建
SomeClass
的构造函数,在其中您想要访问listview
并在中传递
。通过这种方式,您将能够在任何listview
的引用每当您创建SomeClass
实例时,都会使用构造函数class
中访问listview
,例如
MainWindow.xaml.cs 文件中
在您想要访问 listview 的其他类的
One thing you can do is Create a constructor of
SomeClass
in which you want to access thelistview
and pass the reference oflistview
inconstructor
whenever you are creating the instance ofSomeClass
. In this way you will be able to accesslistview
in anyclass
for example
in MainWindow.xaml.cs file
in some other class where you want to access listview
实际上,在类之间传递 UI 控件并不是一个好主意。
您只能在控制窗口的类中编辑控件(例如列表视图、文本框等)。
但是,您可以创建另一个仅拆分主窗口类的分部类。
如果您将某些内容传递给另一个类,您可以通过这种方式传递它:
或者您可以通过方法传递用户控件属性。
希望这有帮助,
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:
Or you can pass the user control properties through methods.
Hope this helps,
在 MainWindow.xaml 中,您可以创建要访问的控件的静态实例;
然后你可以从外部类访问它,例如;
In MainWindow.xaml you can create static instance of control you want to access;
Then you can access it from outside class like;
在 Form2 中创建事件处理程序并在 Form1 中订阅
Create Event Handler in Form2 and subscribe in Form1