在vb6中,我可以轻松地从childwindow获取另一个子窗口的值...例如frm1.textbox1.text = frm2.listview.item.selectedindex ...我怎样才能在wpf中执行此操作?
我有两个名为EmployeProfile的子窗口,另一个是PrintEmpProfile ...在EmployeeProfile窗口中,有一个listview ...我想要的是如果我要点击打印按钮,我可以得到值来自EmployeeProfile listview ....
到目前为止,这就是我所拥有的。此代码在PrintEmpProfile 中DataTable table = new DataTable("EmpIDNumber");
table.Columns.Add("IDNum", typeof(string));
for (int i = 1; i <= 100; i++)
{
table.Rows.Add(new object[] { EmployeeProfile.????? });
}
我不知道如何从EmployeeProfile listview获取所有值。
答案 0 :(得分:0)
您可以创建列表视图的属性。
public ListView EmployeeListView
{
get
{
return IdOfYourListView;
}
set
{
IdOfYourListView = value;
}
}
现在在PrintEmpProfile上创建EmployeeProfile的对象
EmployeeProfile empf = new EmployeeProfile();
ListView MyListView = empf.EmployeeListView;
答案 1 :(得分:0)
每当打开子窗口时,都会将新子窗口的引用放入集合中。我认为MyChild是你在XAML中定义的子窗口,如:
<Window
x:Class="TEST.MyChild"
...
您可以在App.xaml.cs
中定义包含子窗口的静态列表public partial class App : Application
{
public static List<MyChild> Children
{
get
{
if (null == _Children) _Children = new List<MyChild>();
return _Children;
}
}
private static List<MyChild> _Children;
}
无论何时打开子窗口,都将其添加到此集合中,如:
MyChild Child = new MyChild();
App.Children.Add(Child);
Child.Show();
关闭子窗口时,还应从该集合中删除子项。您可以在窗口的Closed事件中执行此操作。用XML定义封闭式通风口:
Closed="MyChild_Closed"
在代码背后:
private void MyChild_Closed(object sender, EventArgs e)
{
// You may check existence in the list first in order to make it safer.
App.Children.Remove(this);
}
每当子窗口想要访问其他子窗口的ListView时,它就会从集合中获取子对象的引用,然后直接调用XAML中定义的ListView。
MyChild ChildReference = App.Children.Where(x => x.Title == "Child Title").FirstOrDefault();
if (null != ChildReference)
{
ChildReference.listview.SelectedIndex = ...
}