English 中文(简体)
WPF:我怎样才能将文本框的文本通过一条线到主窗口?
原标题:WPF: How can I pass the text of textbox in a thread to the main window?
  • 时间:2012-05-26 13:47:41
  •  标签:
  • c#
  • wpf

使用线条技术,由主窗口窗体创建的黑板窗口窗体中有一个子文本框控制器,我想执行此功能:在主窗口窗体中,当我单击按钮(或Enter-key-down)时,它将文字传送到主窗口窗体中。我能做些什么呢?

最佳回答

您需要让 ChildWindow MainWindow 发回消息。 以下示例应有用 :

Code:

An Interface to allow "communication" between windows
public interface IListner
{
    void Send(String message);
}
MainWindow
public partial class MainWindow : Window, IListner
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public void Send(string message)
    {
        // Read the message here.
        // If this code is called from different thread, use "Dispatcher.Invoke()"
    }

    public void OpenAnotherWindow()
    {
        // Since "MainWindow" implements "IListner", it can pass it s own instance to "ChildWindow"
        ChildWindow childWindow = new ChildWindow(this);
    }  
}
ChildWindow:
public partial class ChildWindow : Window
{
    private IListner Listner { get; set; }

    public ChildWindow(IListner listner)
    {
        InitializeComponent();

        Listner = listner;
    }

    private void OnTextBoxTextChanged()
    {
        // This will call "Send" on "MainWindow"
        Listner.Send(TextBox1.Text);
    }
}
问题回答

很快的谷歌会带回来很多结果...

最好的做法可能是,当您创建 Form2 (孩子) 时, 使用一种公共的方法, 您可以在 Form1 (父/母) 中通过, 然后在 Form1 中再次通过同样的方法, 但通过一个字符串, 而不是一个表。 因此, 您最终会遇到类似的情况 :

表格1 (父母):

private void Button1_Click_ShowChildForm(args..)
{
    Form2 frm2 = new Form2();
    frm2.Show();
    frm2.GetInstance(this);
}

public void PassBack(string var)
{
    TextBox1.Text = var;
}

表2 (儿童):

private static Form1 _frm1;

public void GetInstance(Form1 Frm1)
{
    this._frm1 = Frm1;
}

private void Button2_Click_Close(args...)
{
   _frm1.PassBack(this.TextBox2.Text);
   this.Close();
}

类似这样的事情应该做这个把戏。 ) () ()

NB。你也许可以把它整理一下, 如果你真的想的话, 你可以推翻Form2的“显示”方法, 接受一个“表1”的例子, 而不是宣布一个单独的方法, 但你可以理解这个想法。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签