0

I have a UserControl which I want to re-use multiple times within a page. I found a post about a similar problem, but here comes the twist: If I have a TextBox in my control, I can make single properties public, like this:

public string FirstName
{
   get { return privateTextBox.Text; }
   set { privateTextBox.Text = value; }
}

From my main view (which has a view model) I access that property, like such:

MyUserControl personUserControl= new MyUserControl();
personUserControl.FirstName = viewModel.FirstName;

Here is my issue: What if I have a button in the custom user control, which I want to access. I can access its properties, just like the textbox, but what about events (mainly the Click event)?

Currently, my best solution is invoking a delegate event, which is caught by the view model (of the main view) which then invokes another event, but this seems like a quite bad design.

Is it not possible to access events from user controls, in a manner similar of the property access?

P.S. No it is not a mistake that I mixed MVVM and WinForms. This is intended (clever or not).

Edit: The issue is not so much related to WinForms (from what I can tell) as it is related to using multiple layers of UserControls. This rises the question: Should I pass the ViewModel from the top and down through the multiple layers of UserControls, or should I pass the properties of the UserControls upwards?

Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
  • you can make the events visible outside the controls in the same fashion you did with properties. – Paweł Łukasik Sep 25 '17 at 11:54
  • @PawełŁukasik could you give an example? I cannot figure out what to write (i.e. smth like `public Button_Click myClickEvent`, except it should work) – Jakob Busk Sørensen Sep 25 '17 at 12:03
  • rather `public event EventHandler myClickEvent;` but probably if you're using MVVM go with @fabio's answer – Paweł Łukasik Sep 25 '17 at 12:06
  • _I mixed MVVM and WinForms. This is intended (clever or not)_ - Definitely not mistake, Winforms support MVVM(data-binding) pretty well. Not so powerfull as WPF, but can be used in Winforms and even use same viewmodels in WPF too. – Fabio Sep 25 '17 at 12:07
  • Possible duplicate of [MVVM for winforms](https://stackoverflow.com/questions/982978/mvvm-for-winforms) – Sinatr Sep 25 '17 at 12:13
  • As I read it, my issue is indeed related to the possible duplicate, but by no means the same. My issue spawns from using a custom UserControl, inside another UserControl (which is the view). This means that any ViewModel used inside the 2nd layer UserControl must be passed through either: a) The top level ViewModel **or** b) The view which includes the user control. Neither seems very MVVM like? – Jakob Busk Sørensen Sep 25 '17 at 12:34

1 Answers1

3

You can expose events almost the same way you handle other properties:

private Button myButton;

public event EventHandler MyButtonClick
{
    add { myButton.Click += value; }
    remove { myButton.Click -= value; }
}
rs232
  • 1,248
  • 8
  • 16