0

Another subject and a difficulty along with it surfaced while playing with WPF and it lies in an attempt to call a function from within a dynamically added user control passing it a value.
For current example I have a user control that has a property "Secret" and a button in it named "PublishSecret", constructor sets the value of the Secret to a random number. In MainWindow I have another button named "AddSecretContainer" that adds to the window an additional instance of this user control, next to it I have a textbox named "PublishedSecret". A simple method in MainWindow also was defined, here it is:

public void PublishSecret(int secret)
{
    this.PublishedSecret = secret;
}

Now without passing this Secret I could use routed commands with their bubbling effect. But how can I call PublishSecret providing it with the local value of Secret from user control when PublishSecret button was clicked?

Jyrkka
  • 526
  • 1
  • 8
  • 26
  • `dynamically added user control` - how are you "adding" your usercontrols? you should `not` be manipulating the UI in proceural code in WPF. That's what XAML is for. Learn MVVM before you ever write a single line of code in WPF. – Federico Berasategui Nov 19 '13 at 23:29
  • @HighCore lets say you want to add plugins support to you application, or creating tabs for your browser application, these are also dynamically added controls, aren't they? and how would you handle this situation? Stop thinking inside your MVVM box for a second and be more creative. Even VS is now written in WPF, at least some parts of it, and it supports plugins. – Jyrkka Nov 19 '13 at 23:44
  • 2
    Try fire an event in the user control and subscribe MainWindow to this event and call `PublishSecret` in the event handler – lena Nov 20 '13 at 06:48
  • @lena Well, this could be a way, to subscribe upon creation of user control to an event, but I thought maybe there is a more convenient way of doing these things. – Jyrkka Nov 20 '13 at 09:09
  • 1
    @jyrkka there are many ways to implement a `plugin`-based UI, without resorting to horrible crappy winforms-like code behind practices. Learn MVVM. I'm not the one "in the box" – Federico Berasategui Nov 20 '13 at 14:51
  • @HighCore Well, if you can advise on "where to dig", I'll be more than thankful. – Jyrkka Nov 20 '13 at 19:31
  • Search for `MVVM TabControl` and combine what you find with [this](http://www.codeproject.com/Articles/444371/Creating-WPF-Data-Templates-in-Code-The-Right-Way) – Federico Berasategui Nov 20 '13 at 19:33
  • Thanks @HighCore, started digging... – Jyrkka Nov 20 '13 at 19:39
  • @HighCore, this is a really valuable information, thanks and +1 for that, but this doesn't bring me any closer(or maybe I just can't see it yet clearly) to being able to call a method which is part of main window from within a user control that was created now(thanks to you again) properly, using ItemSource and DataTemplate, so please advise... – Jyrkka Nov 20 '13 at 21:42

1 Answers1

0

If your UserControl was added dynamically, then I assume that you have access to a reference of it in your main view model/code behind. This makes your situation nice and simple. You said there is a Button named PublishSecret, well I also assume that clicking it initiates some functionality.

My first suggestion would be the simplest. Put that functionality into a public method which would be called from your Click or ICommand handler and then simply call that method from your main view model:

childUserControl.PublishSecret();

Please let me know if I have misunderstood your problem.


UPDATE >>>

Ok sorry, I've got you now. You can declare a delegate, add a property of that type to your child UserControl and attach a handler in the main view model. Then you can call the delegate and in the handler in the parent view model, you can call your method. For more information on this technique, please see my answer to the Passing parameters between viewmodels question.

Community
  • 1
  • 1
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Can you please give me also your other suggestions?:) Also actually you understood the problem other way round:), I have a method in main view model which has to do some work according to the data(or with the data) it has received as a parameter, and I want to call it from childUserControl. – Jyrkka Nov 20 '13 at 21:59