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. MVVM : Assign ItemSource in View's Code Behind

MVVM : Assign ItemSource in View's Code Behind

Scheduled Pinned Locked Moved WPF
csharpcsswpfwcfarchitecture
5 Posts 4 Posters 0 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.
  • N Offline
    N Offline
    NTheOne
    wrote on last edited by
    #1

    Hello To All, I am new to MVVM with Silverlight. MyTestView --> View (Button, Grid) MyTestViewModel --> ViewModel (WCF Service Methods) In my button call I have assigned the DataContext. I have to fill grid with the data that have been returned from the service call in the same click event in the View's code behind. Any help regarding this will be greatly appreciated.. Thanks in adv...

    S A 2 Replies Last reply
    0
    • N NTheOne

      Hello To All, I am new to MVVM with Silverlight. MyTestView --> View (Button, Grid) MyTestViewModel --> ViewModel (WCF Service Methods) In my button call I have assigned the DataContext. I have to fill grid with the data that have been returned from the service call in the same click event in the View's code behind. Any help regarding this will be greatly appreciated.. Thanks in adv...

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      As in your other post, you are misunderstanding MVVM. All your code belongs in the ViewModel, not in the code behind (there are a few exceptions). You should not be assigning DataContext in button handlers. It should be set once using something like the view locator pattern. Everything that the View gets from the ViewModel is done with data binding, not code in the code behind.

      N 1 Reply Last reply
      0
      • S SledgeHammer01

        As in your other post, you are misunderstanding MVVM. All your code belongs in the ViewModel, not in the code behind (there are a few exceptions). You should not be assigning DataContext in button handlers. It should be set once using something like the view locator pattern. Everything that the View gets from the ViewModel is done with data binding, not code in the code behind.

        N Offline
        N Offline
        NTheOne
        wrote on last edited by
        #3

        Hi.. Thanks for the reply. But I have situation for getting search criteria from the page and perform the operation in the button click event based on passing the search criteria to the view model. So, How can I get view's control value in the viewmodel? Can u pls give a hint about how to overcome such type of situations? Thanks..

        M 1 Reply Last reply
        0
        • N NTheOne

          Hi.. Thanks for the reply. But I have situation for getting search criteria from the page and perform the operation in the button click event based on passing the search criteria to the view model. So, How can I get view's control value in the viewmodel? Can u pls give a hint about how to overcome such type of situations? Thanks..

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

          The search criteria should be bound to a property on the VM. You then have the data in the VM, responding to the event (click the search or the key up/down) need to be handled.

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • N NTheOne

            Hello To All, I am new to MVVM with Silverlight. MyTestView --> View (Button, Grid) MyTestViewModel --> ViewModel (WCF Service Methods) In my button call I have assigned the DataContext. I have to fill grid with the data that have been returned from the service call in the same click event in the View's code behind. Any help regarding this will be greatly appreciated.. Thanks in adv...

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

            Set the view's datacontext to the view model. You could then apply commands to handle the button command click in the view model.

            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.

            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