Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF Custom Control & User Control

WPF Custom Control & User Control

Scheduled Pinned Locked Moved WPF
csharpwpfdesignhelptutorial
10 Posts 4 Posters 18 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    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.

    Graeme_GrantG S L 3 Replies Last reply
    0
    • K Kevin Marois

      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.

      Graeme_GrantG Offline
      Graeme_GrantG Offline
      Graeme_Grant
      wrote on last edited by
      #2

      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

      K 1 Reply Last reply
      0
      • Graeme_GrantG Graeme_Grant

        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

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        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.

        Graeme_GrantG 1 Reply Last reply
        0
        • K Kevin Marois

          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.

          S Offline
          S Offline
          Sandeep Mewara
          wrote on last edited by
          #4

          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...

          K 1 Reply Last reply
          0
          • K Kevin Marois

            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.

            Graeme_GrantG Offline
            Graeme_GrantG Offline
            Graeme_Grant
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • K Kevin Marois

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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

              K 1 Reply Last reply
              0
              • S Sandeep Mewara

                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...

                K Offline
                K Offline
                Kevin Marois
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • L Lost User

                  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

                  K Offline
                  K Offline
                  Kevin Marois
                  wrote on last edited by
                  #8

                  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.

                  L 1 Reply Last reply
                  0
                  • K Kevin Marois

                    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.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    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

                    K 1 Reply Last reply
                    0
                    • L Lost User

                      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

                      K Offline
                      K Offline
                      Kevin Marois
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups