我需要将值传递给子窗口。在子窗口中有两个文本框。我需要在子窗口文本框中显示值,同时打开它。
我尝试了以下方式,My Child窗口类如下,
public partial class ChildWindow:ChildWindow
{
public int abc {get;set;}
public string value{get;set;}
public ChildWindow()
{
InitializeComponent();
this.txtbox1.Text = abc ;
this.txtbox2.Text = value;
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
我的父窗口如下,
private void EditButton_Click(System.Object sender, System.Windows.RoutedEventArgs e)
{
ChildWindow child= new ChildWindow();
child.abc = 1;
child.alue = "Hello"
child.show();
}
如何在子窗口控件打开时显示带有值的子窗口控件(来自Parent)?
答案 0 :(得分:1)
您可以创建overload
constructor
。
public ChildWindow(string abc,string value)
{
InitializeComponent();
this.txtbox1.Text = abc ;
this.txtbox2.Text = value;
}
比如这样创建{child}的object
ChildWindow child= new ChildWindow("abc","somevalue");
答案 1 :(得分:1)
您可以更改以下内容:
public int abc {get;set;}
public string value{get;set;}
致:
public int abc
{
get
{
int result = 0;
int.TryParse(this.txtbox1.Text, out result);
return result;
}
set
{
this.txtbox1.Text = value;
}
}
public string value
{
get
{
return this.txtbox2.Text;
}
set
{
this.txtbox2.Text = value;
}
}
代码中的问题是,在控件初始化期间正在分配属性。不是在属性发生变化时。