我有两个窗口windowA
,其中有一个按钮可以打开windowB
,而windowB
有一个按钮可以自行关闭并返回List值。我试过这段代码,但值保持为null。 windowB
有RadGridView控件,我想从中获取selectedItem并将其添加到列表中。
public class WindowA : Window
{
...
private void button_Click(object sender, RoutedEventArgs e)
{
WindowB winB = new WindowB();
if (winB.ShowDialog() == false)
{
listClass lc = winB.SelectedItemButton;
...
}
}
}
public class WindowB : Window
{
...
public listClass SelectedItemButton
{
get { return selectedItem; }
set
{
selectedItem = ((listClass)AGridView.SelectedItem);
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
结果是一个listClass,但里面没有值。为什么?以及如何使selectedItem = ((listClass)AGridView.SelectedItem);
此行适用于另一个窗口?
答案 0 :(得分:0)
您的一个例子:
public partial class MainWindow : Window
{
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 dlg = new Window1();
if(dlg.ShowDialog()??false)
{
MessageBox.Show(dlg.S);
}
}
}
// Dialog
public partial class Window1 : Window
{
public string S
{
get
{
return this.txt1.Text;
}
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
}
答案 1 :(得分:0)
你应该在Window1中创建listClass变量,当你打开Window2时,你应该将这个变量作为参数传递。这是我的演示:
第一个窗口:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TestClass testClass = new TestClass();
testClass.Test = "Initial";
Second second = new Second(testClass);
second.ShowDialog();
label.Content = testClass.Test; // It prints "Changed"
}
}
第二个窗口:
public partial class Second : Window
{
TestClass testClass;
public Second(TestClass sent)
{
InitializeComponent();
testClass = sent;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
testClass.Test = "Changed"; // Change the value
}
}
我的testClass(listClass):
public class TestClass
{
public string Test { get; set; }
}