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?