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. WCF and WF
  4. Setting DataContext in code and XAML

Setting DataContext in code and XAML

Scheduled Pinned Locked Moved WCF and WF
helpwpfdiscussion
2 Posts 2 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.
  • P Offline
    P Offline
    Phil J Pearson
    wrote on last edited by
    #1

    I have a window with several bound items. Initially I set the DataContext in xaml like this:

    <Window.DataContext>
        <local:UserManagerViewModel />
    </Window.DataContext>
    

    Everything is fine. The problem is I want to have the same ViewModel accessible to more than one window without instantiating more than one copy of the view model. So I want to set the DataContext in code to the one-and-only view model. Therefore I want to specify the DataContext differently in xaml so that it knows what class the data will be without instantiating the view model. I don't know the right way to do this. Please help.

    Phil


    The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

    K 1 Reply Last reply
    0
    • P Phil J Pearson

      I have a window with several bound items. Initially I set the DataContext in xaml like this:

      <Window.DataContext>
          <local:UserManagerViewModel />
      </Window.DataContext>
      

      Everything is fine. The problem is I want to have the same ViewModel accessible to more than one window without instantiating more than one copy of the view model. So I want to set the DataContext in code to the one-and-only view model. Therefore I want to specify the DataContext differently in xaml so that it knows what class the data will be without instantiating the view model. I don't know the right way to do this. Please help.

      Phil


      The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      If you really only ever have one instance of the view model You have two options: make UserManagerViewModel static, or turn it into a Singleton http://www.yoda.arachsys.com/csharp/singleton.html[^] Another strategy would be to create the object as a resource (possibly in the app.xaml) and refer child-windows to that.

      CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

      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