【问题标题】:Pass value to Child Window form Parent Page将值传递给父页面的子窗口
【发布时间】:2015-07-27 05:04:30
【问题描述】:

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

我尝试了以下方式,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();
    }

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

【问题讨论】:

    标签: c# winforms silverlight silverlight-3.0 silverlight-5.0


    【解决方案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;
        }
    
    }
    

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

    【讨论】:

    • 非常感谢...这正是我所期望的
    【解决方案2】:

    您可以创建overloadconstructor

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

    比像这样创建子窗口的object

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

    【讨论】:

      猜你喜欢
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多