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. Commands in User Control

Commands in User Control

Scheduled Pinned Locked Moved WPF
wpfwcfhelptutorialquestion
7 Posts 5 Posters 1 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.
  • E Offline
    E Offline
    eddieangel
    wrote on last edited by
    #1

    I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA

    M A A P 4 Replies Last reply
    0
    • E eddieangel

      I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      After reading your convoluted question I can sympathise completely. I think, your ITEMSOURCE for the datagrid is the collection, your DATACONTEXT of the control is where the command should reside. This stuff is completely different from what I am used to!

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • E eddieangel

        I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA

        A Offline
        A Offline
        Arun Jacob
        wrote on last edited by
        #3

        If you use datagrid and you want a command inside the datagrid, then you should define that command in the ObservableCollection item object. eg, If you use ObservableCollection<Customer> as ItemsSource, define the command in Customer class.

        Arun Jacob My Technical Blog : Code.NET

        E 1 Reply Last reply
        0
        • E eddieangel

          I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          One way I have found to solve this is to bind your command to the parent datasource in the xaml. For e.g. cmd:mycommand="{Binding ElementName=LayoutRoot, Path=DataContext.RevertClick}". LayoutRoot is what would be the main layout grid in the xaml (added by default)....but you could have used a different name.

          The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

          1 Reply Last reply
          0
          • A Arun Jacob

            If you use datagrid and you want a command inside the datagrid, then you should define that command in the ObservableCollection item object. eg, If you use ObservableCollection<Customer> as ItemsSource, define the command in Customer class.

            Arun Jacob My Technical Blog : Code.NET

            E Offline
            E Offline
            eddieangel
            wrote on last edited by
            #5

            I had considered that as well, but the class is an entity created by the entity framework directly from an SQL table and I thought that overriding that class to add a command would be a little bit more spaghetti than I wanted to deal with. Still, that seems to me like the best working solution. Thank you for the advice. Cheers, --EA

            1 Reply Last reply
            0
            • E eddieangel

              I am having an issue binding a command to a user control. My main window calls a workspace viewmodel that supplies data via observable collection to a datagrid in the workspace viewmodel. In this datagrid I put a button, but am having an issue figuring out how to bind a command to it. The datagrid itemssource is an observable collection from a viewmodel. I have a command on that same view model that tells the datagrid what to do when a button field is pressed. I bound to that name but it isn't working because. I am guessing this is because the datagrid (Which is on a user control) is bound to the observable collection of data and the command is outside the scope of that collection. So on what viewmodel do I put my command in order to execute it on my usercontrol and how to I reference it on my user control? Sorry for the vagueness of it all, this is a bit difficult to explain. Cheers, --EA

              P Offline
              P Offline
              PumbaPumba
              wrote on last edited by
              #6

              Are the columns auto-generated?

              E 1 Reply Last reply
              0
              • P PumbaPumba

                Are the columns auto-generated?

                E Offline
                E Offline
                eddieangel
                wrote on last edited by
                #7

                Not auto generated. The answer was to reference the command on the viewmodel using relativesource - findancestor. Nice and easy. Thanks for the help.

                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