Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7412183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:30:10+00:00 2026-05-29T06:30:10+00:00

So someone suggested using a WPF TreeView , and I thought: Yeah, that seems

  • 0

So someone suggested using a WPF TreeView, and I thought: “Yeah, that seems like the right approach.” Now, hours and hours later, I simply can’t believe how difficult it has been to use this control. Through a bunch of research, I was able to get the TreeView` control working, but I simply cannot find the “proper” way to get the selected item to the view model. I do not need to set the selected item from code; I just need my view model to know which item the user selected.

So far, I have this XAML, which isn’t very intuitive on its own. This is all within the UserControl.Resources tag:

<CollectionViewSource x:Key="cvs" Source="{Binding ApplicationServers}">
    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="DeploymentEnvironment"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

<!-- Our leaf nodes (server names) -->
<DataTemplate x:Key="serverTemplate">
    <TextBlock Text="{Binding Path=Name}"/>
</DataTemplate>

<!-- Note: The Items path refers to the items in the CollectionViewSource group (our servers).
           The Name path refers to the group name. -->
<HierarchicalDataTemplate x:Key="categoryTemplate"
                          ItemsSource="{Binding Path=Items}"
                          ItemTemplate="{StaticResource serverTemplate}">
    <TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>
</HierarchicalDataTemplate>

And here’s the treeview:

<TreeView DockPanel.Dock="Bottom" ItemsSource="{Binding Source={StaticResource cvs}, Path=Groups}"
              ItemTemplate="{StaticResource categoryTemplate}">
            <Style TargetType="TreeViewItem">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected}"/>
            </Style>
        </TreeView>

This correctly shows servers by environment (dev, QA, prod). However, I’ve found various ways on SO to get the selected item, and many are convoluted and difficult. Is there a simple way to get the selected item to my view model?

Note: There is a SelectedItem property on the TreeView`, but it’s read-only. What’s frustrating to me is that read-only is just fine; I don’t want to change it via code. But I can’t use it because the compiler complains that it’s read-only.

There was also a seemingly elegant suggestion to do something like this:

<ContentPresenter Content="{Binding ElementName=treeView1, Path=SelectedItem}" />

And I asked this question: “How can your a view model get this information? I get that ContentPresenter holds the selected item, but how do we get that over to the view model?” But there is no answer yet.

So, my overall question is: “Is there a simple way to get the selected item to my view model?”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:30:11+00:00Added an answer on May 29, 2026 at 6:30 am

    To do what you want you can modify the ItemContainerStyle of the TreeView:

    <TreeView>
      <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
          <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
        </Style>
      </TreeView.ItemContainerStyle>
    </TreeView>
    

    Your view-model (the view-model for each item in the tree) then has to expose a boolean IsSelected property.

    If you want to be able to control if a particular TreeViewItem is expanded you can use a setter for that property too:

    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
    

    Your view-model then has to expose a boolean IsExpanded property.

    Note that these properties work both ways so if the user selects a node in the tree the IsSelected property of the view-model will be set to true. On the other hand if you set IsSelected to true on a view-model the node in the tree for that view-model will be selected. And likewise with expanded.

    If you don’t have a view-model for each item in the tree, well, then you should get one. Not having a view-model means that you are using your model objects as view-models, but for this to work these objects require an IsSelected property.

    To expose an SelectedItem property on your parent view-model (the one you bind to the TreeView and that has a collection of child view-models) you can implement it like this:

    public ChildViewModel SelectedItem {
      get { return Items.FirstOrDefault(i => i.IsSelected); }
    }
    

    If you don’t want to track selection on each individual item on the tree you can still use the SelectedItem property on the TreeView. However, to be able to do it “MVVM style” you need to use a Blend behavior (available as various NuGet packages – search for “blend interactivity”).

    Here I have added an EventTrigger that will invoke a command each time the selected item changes in the tree:

    <TreeView x:Name="treeView">
      <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectedItemChanged">
          <i:InvokeCommandAction
            Command="{Binding SetSelectedItemCommand}"
            CommandParameter="{Binding SelectedItem, ElementName=treeView}"/>
        </i:EventTrigger>
      </i:Interaction.Triggers>
    </TreeView>
    

    You will have to add a property SetSelectedItemCommand on the DataContext of the TreeView returning an ICommand. When the selected item of the tree view changes the Execute method on the command is called with the selected item as the parameter. The easiest way to create a command is probably to use a DelegateCommand (google it to get an implementation as it is not part of WPF).

    A perhaps better alternative that allows two-way binding without the clunky command is to use BindableSelectedItemBehavior provided by Steve Greatrex here on Stack Overflow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, I have a WPF TreeView that has three nodes, I would like
As part of another question that did involve programming (I assure you), someone suggested
I'm setting up a login system for a site and someone suggested using openID
In response to another question of mine, someone suggested that I avoid long lines
I browsed around and saw that MFMessageComposeViewController doesn't support MMS, however some suggested using
Someone suggested moving a table full of settings, where each column is a setting
I have a three tier set-up. Someone suggested I should get the ConnectionString from
Is there a log4net version built against Silverlight somewhere? Failing that, can someone suggest
Edit... Someone suggested I do it differently... If I were to use .htaccess, would
Suppose someone is using a 64-bit OS and a 64-bit JVM. Does using the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.