Reputation: 498
I hope this doesn't seem like a weird question or something that should be avoided, but is it possible to make the instance of the control public so that it can be used in other classes?
For instance, in XAML the User Control is established by
<local:MyControl x:Name="ControlName" />
Which can be accessed on the same code-behind by simply calling ControlName.UseMethod();
just like any instances of classes that I would make.
Now, while I could declare the instance of normal classes in the code-behind as public then use it freely, I don't have the option to do so here as it is being declared in the XAML which I can't make public directly.
I need to be able to access the contents of the User Control throughout other sections of the program and unable to use the methods I need as static.
My experience in programming is very little, learning from books and through small projects, which may explain any oddities in the question. Hope it's okay.
Thanks
Upvotes: 1
Views: 1614
Reputation: 61369
You absolutely cannot just make that control public. Controls in .NET are private member variables of their Form
, Page
, Window
, etc. They are generated by the compiler into the appropriate "hidden" cs file, and are set to private at that point. You can expose them though, with a property that returns them:
public MyControl MyControlProperty { get { return myControlName; } }
Now, you should really never be doing this. Other classes shouldn't know the internals of your view classes, they should be invoking methods, changing data, etc to cause those internals to change.
Also, as this is WPF, you shouldn't even be doing that, instead you should use view models that can invoke methods on other view models (often through a model) to cause their associated views to react and update.
In short, yes, you can expose controls to the world, but if you have to do so, you are almost assuredly doing it totally wrong.
Upvotes: 2