WPF Custom Control & User Control
-
When you design a CustomControl or UserControl, how do you fire off a method inside it? Say for example you create a control that needs to load some data async. So you put the control on a Window and start the window. There's no way to call into the control and call 'Load()'. You can bind to it, but what would you bind? You could have it listen to an event. Is there some other way?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
When you design a CustomControl or UserControl, how do you fire off a method inside it? Say for example you create a control that needs to load some data async. So you put the control on a Window and start the window. There's no way to call into the control and call 'Load()'. You can bind to it, but what would you bind? You could have it listen to an event. Is there some other way?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
You can use async but you may be on a different thread to the UI when trying to update data bound to the UI. You need to marshall to the UI thread or you will get cross-thread exceptions. My previous answer where I point to a series of WPF YT videos will answer this question for you.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
-
You can use async but you may be on a different thread to the UI when trying to update data bound to the UI. You need to marshall to the UI thread or you will get cross-thread exceptions. My previous answer where I point to a series of WPF YT videos will answer this question for you.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
Not sure what you mean by this response. I'm asking how to create a UserControl or CustomControl, place it on a Window, then execute a method on it from a button click on the Window.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
When you design a CustomControl or UserControl, how do you fire off a method inside it? Say for example you create a control that needs to load some data async. So you put the control on a Window and start the window. There's no way to call into the control and call 'Load()'. You can bind to it, but what would you bind? You could have it listen to an event. Is there some other way?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
You expose the events for that usercontrol/custom control. Now treat them like any other standard control. Place them in your form and then define a method for the exposed events. An example here: [Exposing Custom event from custom control ](https://www.codeproject.com/Articles/417682/Exposing-Custom-event-from-custom-control) [ClientCallback custom control for web applications](https://www.codeproject.com/Articles/31889/ClientCallback-custom-control-for-web-applications)
Latest CodeProject post: Quick look into Machine Learning workflow How to solve Word Ladder Problem? To read all my blog posts, visit: Learn by Insight...
-
Not sure what you mean by this response. I'm asking how to create a UserControl or CustomControl, place it on a Window, then execute a method on it from a button click on the Window.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Kevin Marois wrote:
I'm asking how to create a UserControl or CustomControl, place it on a Window, then execute a method on it from a button click on the Window.
Okay, that is a lot clearer and easier to understand what you want. I wrote an article that walks through extending a Control: Flexible WPF ToggleSwitch Lookless Control in C# & VB[^]. UserControls are just like another Window to use.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
-
When you design a CustomControl or UserControl, how do you fire off a method inside it? Say for example you create a control that needs to load some data async. So you put the control on a Window and start the window. There's no way to call into the control and call 'Load()'. You can bind to it, but what would you bind? You could have it listen to an event. Is there some other way?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
You give the user control a .Name in the XAML; the .Name is available to the Window's code behind. If you want to access the UC outside of the Window, you need to add a public reference to the UC; e.g. public Type UC => (name of UC in XAML). In those cases where the UC is a (global) singleton, I (may) give it a public static reference to its instance; the UC can then be accessed by any part of the app in that case.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
You expose the events for that usercontrol/custom control. Now treat them like any other standard control. Place them in your form and then define a method for the exposed events. An example here: [Exposing Custom event from custom control ](https://www.codeproject.com/Articles/417682/Exposing-Custom-event-from-custom-control) [ClientCallback custom control for web applications](https://www.codeproject.com/Articles/31889/ClientCallback-custom-control-for-web-applications)
Latest CodeProject post: Quick look into Machine Learning workflow How to solve Word Ladder Problem? To read all my blog posts, visit: Learn by Insight...
I'm not asking about events. I'm asking how to call the Load method on my UserControl from the Window's ViewModel. The UserControl uis a stand along control. It needs to have Load called to load up the data. The Window's ViewModel doesn't know about the UserControl.
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
You give the user control a .Name in the XAML; the .Name is available to the Window's code behind. If you want to access the UC outside of the Window, you need to add a public reference to the UC; e.g. public Type UC => (name of UC in XAML). In those cases where the UC is a (global) singleton, I (may) give it a public static reference to its instance; the UC can then be accessed by any part of the app in that case.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
There is no code behind. I'm using MVVM
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
There is no code behind. I'm using MVVM
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
You could have said that at the outset: "I will only entertain MVVM solutions". Noted.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
-
You could have said that at the outset: "I will only entertain MVVM solutions". Noted.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
Sorry for the confusion. I just asumed that MVVM is the way most folks do things. So I found out a way to load my control:
public override void OnApplyTemplate()
{
base.OnApplyTemplate();Load();
}
Not sure if this is the best way, but it works. Thanks
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.