繁体   English   中英

将值传递给子窗口表单父页面

[英]Pass value to Child Window form Parent Page

我需要将值传递给子窗口。在子窗口中有两个文本框。我需要在子窗口中打开文本框时显示该值。

我尝试了以下方式,“我的孩子”窗口类如下所示:

    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();
    }

打开子窗口时,如何显示带有值的子窗口控件(从父窗口获取)?

您可以创建constructoroverload

public ChildWindow(string abc,string value)
{
InitializeComponent();          
this.txtbox1.Text =  abc ; 
this.txtbox2.Text =  value; 
}

比这样创建子窗口的object

ChildWindow child= new ChildWindow("abc","somevalue");

您可以更改以下内容:

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;
    }

}

您的代码中的问题是,在控件初始化期间分配了属性。 更改属性时不可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM