kez
kez

Reputation: 2313

modify a value of a user control property from another window

I have following user control

public partial class LayoutWindow : UserControl
{
 ....
}

on this user-control I have a button to open an another window call PopUp.

So I'm trying to update LayoutWindow UserControl diagram.Bounds property(which is canvas size), once button(OkButton_Click) click inside PopUp Window

So I tried following thing

 public partial class PopUp : Window
 {
    private void OkButton_Click(object sender, RoutedEventArgs e)
    {          

          LayoutWindow lw= new LayoutWindow();
          lw.InitializeComponent();
          lw.diagram.Bounds = new Rect(0, 0, 400, 400);
          Close();
    }
} 

but in this way it's not updating above propery, how can I do this properly

Upvotes: 2

Views: 254

Answers (1)

mm8
mm8

Reputation: 169150

You are creating a new instance of the LayoutWindow in the PopUp window. You need to access the already existing instance. The easiest way to do this would probably be to inject the PopUp window with an instance of LayoutWindow when you create it:

public partial class PopUp : Window
{
    private readonly LayoutWindow _lw;
    public PopUp(LayoutWindow lw)
    {
        InitializeComponent();
        _lw = lw;
    }

    private void OkButton_Click(object sender, RoutedEventArgs e)
    {
        _lw.diagram.Bounds = new Rect(0, 0, 400, 400);
        Close();
    }
}

LayoutWindow.xaml.cs:

var popup = new PopUp(this);
popup.Show();

Upvotes: 2

Related Questions