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. How to make a TreeView’s and ListBox’s SelectedItem stay in sync? (in an MVVM app)

How to make a TreeView’s and ListBox’s SelectedItem stay in sync? (in an MVVM app)

Scheduled Pinned Locked Moved WCF and WF
wpfdata-structuresarchitecturetutorialquestion
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.
  • S Offline
    S Offline
    Steve The Plant
    wrote on last edited by
    #1

    Hi, I'm certain this has come up before, but I haven't been able to find the answer. I made a basic ViewModel that contains a list of People (an array of Person) with a property called SelectedPerson, which naturally points to the currently selected Person in the list of People. I also have a ListBox and a TreeView that are databound to the ViewModel's People list. What I'd like to do is to keep the ListBox's SelectedValue and TreeView's SelectedItem in sync with with the ViewModel's SelectedPerson. The idea is that no matter how the SelectedPerson is modified (through a control, through code, etc), all the controls should update themselves properly by selecting the correct item. I can get it to work with two ListBoxes, which is nice, but I can't get it to work with a ListBox and a TreeView because the TreeView's SelectedItem is readonly and apparently unavailable through XAML. Where should I look to get ideas on making this work? Also note that I'm trying to make this work in pure XAML. No code-behind as XAML files in my application can be loaded and changed dynamically. Thanks!

    L 1 Reply Last reply
    0
    • S Steve The Plant

      Hi, I'm certain this has come up before, but I haven't been able to find the answer. I made a basic ViewModel that contains a list of People (an array of Person) with a property called SelectedPerson, which naturally points to the currently selected Person in the list of People. I also have a ListBox and a TreeView that are databound to the ViewModel's People list. What I'd like to do is to keep the ListBox's SelectedValue and TreeView's SelectedItem in sync with with the ViewModel's SelectedPerson. The idea is that no matter how the SelectedPerson is modified (through a control, through code, etc), all the controls should update themselves properly by selecting the correct item. I can get it to work with two ListBoxes, which is nice, but I can't get it to work with a ListBox and a TreeView because the TreeView's SelectedItem is readonly and apparently unavailable through XAML. Where should I look to get ideas on making this work? Also note that I'm trying to make this work in pure XAML. No code-behind as XAML files in my application can be loaded and changed dynamically. Thanks!

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

      Expose a CollectionViewSource on your ViewModel. The use the IsSynchronizedWithCurrentitem property on the two controls. I know it sounds simple, but it works and was designed to be simple.

      Cheers, Karl » CodeProject 2008 MVP, CodeProject 2009 MVP My Blog | Mole's Home Page | XAML Power Toys Home Page

      Just a grain of sand on the worlds beaches.

      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