How can I access one window control (richtextbox) from another window in wpf?

I am sure this is something very simple, but I cannot understand. I searched here and on msdn and could not find the answer. I need to set richtextboxes selection through richtextbox.Selection.Select (TextPointer1, Textpointer2).

+5
source share
4 answers

Application.Current contains a collection of all the windows in your application, you can get another window with a request, for example

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
+14
source
Application.Current.Windows.OfType(Of MainWindow).First
+5
source

texbox , , , RichTextBox ()

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

Window1 Window2 , , . .

, , - Window1, :

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

Window2 :

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

select 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

All Articles