如何从wpf中的另一个窗口访问一个窗口的控件(richtextbox)?

时间:2022-08-26 21:35:00

I'm sure this is something very simple but I can't figure it out. I've searched here and on msdn and have been unable to find the answer. I need to be able to set the richtextboxes selection via richtextbox.Selection.Select(TextPointer1, Textpointer2).

我确信这很简单,但我无法弄清楚。我在这里和msdn上搜索过,但一直无法找到答案。我需要能够通过richtextbox.Selection.Select(TextPointer1,Textpointer2)设置richtextboxes选项。

4 个解决方案

#1


15  

Application.Current contains a collection of all windows in you application, you can get the other window with a query such as

Application.Current包含您应用程序中所有窗口的集合,您可以通过查询获取其他窗口

var window2 = Application.Current.Windows
    .Cast<Window>()
    .FirstOrDefault(window => window is Window2) as Window2;

and then you can reference the control from your code, as in

然后你可以从代码中引用控件,如

var richText = window2.MyRichTextBox

#2


5  

Application.Current.Windows.OfType(Of MainWindow).First

#3


2  

You cant access the texbox from another window as it is private to that window you can however work around this by exposing the RichTextBox as a public property on your window (hack)

您无法从另一个窗口访问texbox,因为它是该窗口专用的,但您可以通过将RichTextBox作为公共属性暴露在窗口(hack)来解决此问题。

public RichTextBox RichTextBox {
  get{
    //the RichTextBox would have a property x:Name="richTextbox" in the xaml
    return richTextBox;
  }
}

#4


1  

You should be able to access controls on Window1 from Window2 code behind, if that's what you want. Generated fields are internal by default.

你应该可以从后面的Window2代码访问Window1上的控件,如果这是你想要的。生成的字段默认为内部字段。

All you need is to name the control on Window1, like this:

您只需要在Window1上命名控件,如下所示:

<RichTextBox x:Name="richtextbox" ... />

In Window2 code behind:

在Window2代码后面:

var window = new Window1(); // or use the existing instance of Window1
window.richtextbox.Selection.Select(TextPointer1, Textpointer2);

A better option would be to encapsulate select operation in a method in code behind of Window1, to avoid giving away internal. Then you would have:

更好的选择是将选择操作封装在Window1后面的代码中的方法中,以避免泄露内部。然后你会有:

// Window1.cs
public void Select(int param1, int param2)
{
    richtextbox.Selection.Select(param1, param2);
}

// Window2.cs
var window = new Window1(); // or use the existing instance of Window1
window.Select(TextPointer1, Textpointer2);

#1


15  

Application.Current contains a collection of all windows in you application, you can get the other window with a query such as

Application.Current包含您应用程序中所有窗口的集合,您可以通过查询获取其他窗口

var window2 = Application.Current.Windows
    .Cast<Window>()
    .FirstOrDefault(window => window is Window2) as Window2;

and then you can reference the control from your code, as in

然后你可以从代码中引用控件,如

var richText = window2.MyRichTextBox

#2


5  

Application.Current.Windows.OfType(Of MainWindow).First

#3


2  

You cant access the texbox from another window as it is private to that window you can however work around this by exposing the RichTextBox as a public property on your window (hack)

您无法从另一个窗口访问texbox,因为它是该窗口专用的,但您可以通过将RichTextBox作为公共属性暴露在窗口(hack)来解决此问题。

public RichTextBox RichTextBox {
  get{
    //the RichTextBox would have a property x:Name="richTextbox" in the xaml
    return richTextBox;
  }
}

#4


1  

You should be able to access controls on Window1 from Window2 code behind, if that's what you want. Generated fields are internal by default.

你应该可以从后面的Window2代码访问Window1上的控件,如果这是你想要的。生成的字段默认为内部字段。

All you need is to name the control on Window1, like this:

您只需要在Window1上命名控件,如下所示:

<RichTextBox x:Name="richtextbox" ... />

In Window2 code behind:

在Window2代码后面:

var window = new Window1(); // or use the existing instance of Window1
window.richtextbox.Selection.Select(TextPointer1, Textpointer2);

A better option would be to encapsulate select operation in a method in code behind of Window1, to avoid giving away internal. Then you would have:

更好的选择是将选择操作封装在Window1后面的代码中的方法中,以避免泄露内部。然后你会有:

// Window1.cs
public void Select(int param1, int param2)
{
    richtextbox.Selection.Select(param1, param2);
}

// Window2.cs
var window = new Window1(); // or use the existing instance of Window1
window.Select(TextPointer1, Textpointer2);