.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

An Expression Blend behavior to return the TreeView SelectedItem

Posted By:      Posted Date: October 04, 2010    Points: 0   Category :WPF

Like all developers I eventually needed the SelectedItem of a TreeView in my ViewModel.  My requirement involved allowing a MenuItem to be enabled or disabled depending upon which TreeView node was selected.  My solution is a behavior based off of a forum answer provided by the good Dr. WPF.  The code  for the TreeViewSelectedItemBehavior class and some simple examples of using it are below:  Any suggestions for improvements are welcome. 

    public class TreeViewSelectedItemBehavior : Behavior<TreeView>
        public TreeViewSelectedItemBehavior()
        public static readonly DependencyProperty SelectedTreeViewItemProperty = DependencyProperty.Register("SelectedTreeViewItem", typeof(System.Windows.Controls.TreeViewItem), typeof(TreeViewSelectedItemBehavior), new PropertyMetadata(null));

        public System.Windows.Controls.TreeViewItem SelectedTreeViewItem
            get { return this.GetValue(TreeViewSelectedItemBehavior.SelectedTreeViewItemProperty) as System.Windows.Controls.TreeViewItem; }
            set { this.SetValue(TreeViewSelectedItemBehavior.SelectedTreeViewItemProperty, value); }

        protected override void OnAttached()

View Complete Post

More Related Resource Links

Silverlight: Create Animations with XAML and Expression Blend


In this excerpt from his upcoming book, Laurence Moroney explains the basics of Silverlight animation and the animation tools available in Expression Blend.

Lawrence Moroney

MSDN Magazine August 2008

Dataform in Expression Blend

Can Expression Blend 4 handle a Dataform control? I added a Dataform control to my xaml in VS 2010 and when I now open the same project in Blend it throws an exception. Also the reason i added it in VS 2010 was that I could not find the Dataform control in Blend. Am I missing a reference/namespace etc...?

Where is "projection" in Expression Blend 4???

I've just installed the demo version of Blend and I created a new wpf (.net 4) project. I drew a rectangle and now I want to change it's projection values. I can't find it in the "properties" anywhere, nor how to make it show there... Help????   Thanks

ListBox.SelectedItem stuck at whatever is first selected and other strange ListBox behavior

Hi, I've built a List<Participant> (Participant being my custom type) and assigned this to ListBox.ItemsSource. (I'm aware this is more the Windows Forms way than the WPF way of databinding, but I'd still like to understand what's going on here - even though suggestions on how to perform my task in a more WPF-y way are also welcome.) I've set ListBox.SelectionMode to Single, and attached an event handler to the SelectionChanged event. In this handler, I dump the SelectedIndex and SelectedItem to debug output. Contrary to my expectation, 1) SelectedIndex is always -1. This isn't really causing me any trouble, but it's certainly not what I'd expect. 2) SelectedItem refers to the correct object the first time (in the windows lifetime) I select an item, but then keeps referring to this first-selected item regardless of whether I deselect (by clicking the item again while holding down the CTRL key) or select some other item. 3) If I keep clicking around and scrolling a bit (the list has ~320 items) within the list, now and then the list suddenly displays a bunch of items - sometimes many in a row, sometimes non-contiguous items, but it seems always "nearby" items! - as if they were selected. The list keeps firing the SelectionChanged event, but SelectedIndex is forever -1 and SelectedItem (and SelectedValue) always refers to whatever I had selected the fir

XPath expression not working for Google Maps return XML

I have this XMLdocument:<kml><Response><name>3051,netherlands</name><Status><code>200</code><request>geocode</request></Status><Placemark id="p1"><address>3051 Rotterdam, The Netherlands</address><AddressDetails Accuracy="5"><Country><CountryNameCode>NL</CountryNameCode><CountryName>Nederland</CountryName><AdministrativeArea><AdministrativeAreaName>Zuid-Holland</AdministrativeAreaName><Locality><LocalityName>Rotterdam</LocalityName><PostalCode><PostalCodeNumber>3051</PostalCodeNumber></PostalCode></Locality></AdministrativeArea></Country></AddressDetails><ExtendedData><LatLonBox north="51.9497984" south="51.9402954" east="4.4907255" west="4.4656367"/></ExtendedData><Point><coordinates>4.4767883,51.9458650,0</coordinates></Point></Placemark></Response></kml>I used this site: http://www.xmlme.com/XpathTool.aspxwhere I enter the expression: "kml/Response/Placemark/Point/coordinates"I then get the right value. HOWEVER, when I place this code in .NET, the xpath expression returns nothing:req = HttpWebRequest.Create("http://maps.google.com/maps/geo?output=x

TreeView, ContextMenu, how do I get the SelectedItem's data Node/object?


Hi all,

I've implemented a tree and have bound it to my treeview. the binding is fine, but id like to add "add child" "remove node" functionality in a context menu for a selected item. I can not for the life of me figure out how to do this. My code is below. please help me. please point me in the right direction.


<TreeView Name="treeView" Margin="10,10,14,10" BorderThickness="2" ItemsSource="{Binding Nodes}">

"Could not find default endpoint element that references contract " @ Expression Blend @ Design Time


so, in Expression Blend, and only there, during design time i get an error:

"System.InvalidOperationException was unhandled by user code

Could not find default endpoint element that references contract 'GBAUploadService.IGBAUploadService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."

this doesn't happen in Visual Studio 2010, and of course the service itself works flawlessly.

any ideas?


the code triggering the exception is part of a ObjectDataProvider, which consumes a web service in code-behind:


  public class FileGroupsDataProvider
    public BaseUploadJob[] GetFileGroups()
      if (Assembly.GetEntryAssembly() != null)
        using (GBAUploadServiceClient gbaUploadServiceClient = new GBAUploadServiceClient())
          BaseUploadJob[] baseUploadJobs = gbaUploadServiceClient.GetFileGroups();
          return baseUploadJobs;
      return null;

Treeview SelectedItem



Can someone help me with treeview selectedItem. In winforms there is $this.SelectedNode but how Can I do that in WPF. I´m writing GUI in XAML and code behind is powershell.

Thank you,


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



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.


"Open in Expression Blend" feature in Visual Studio does not work

When I right click on a xaml I do not see the option "Open in Expression Blend". I have installed and reinstalled and reinstalled ... Expression Studio Ultimate (licensed under Dreamspark) but I saw no solution for this problem.

How "uninstall the Expression Blend 4 sdk"?


I've got the problem described here http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/406c3c85-e541-4cd2-82ad-15da392d180b and here http://social.msdn.microsoft.com/Forums/en-US/vswpfdesigner/thread/2cfaf28f-80af-49ee-8408-07b4a8e0e964.

In the latter thread it is suggested that uninstalling Expression Blend 4 SDK seems to help. How would I do that? I don't seem to find it in Control Panel, Add or Remove Programs, Visual Studio 2010 nor anywhere in the VS 2010 menus.

I have VS 2010 Ultimate with no add-ons or extensions.



Changing background Color of a custom slider when IsEnabled = False (Expression Blend)


I have a customized slider control. When the control is set to IsEnabled=false, the color changes to the default desabled color. How do I change this to a different color in Expression blend? I have tried unchecking IsEnabled property in Expression Blend, and changing the color, but is still takes the default disabled color. 


Would appreciate if anyone knows how to do this using Expression Blend


Thank you


Design view missing in expression blend 4

When i open my project (its not a silverlight project btw just a standard)  i can only see my code and the xaml but no actual design view, but if i open the project in Visual Studio i can see my forms, this is really annoying as im in the middle of a project and also if i open other projects in EB i can see them fine so just his project, any help please?

Custom Editing Behavior for DataGridView TextBox Columns

For various reasons I'm using a DataGridView instead of a ListView, and while configuring the DataGridView to look like a ListView wasn't much of a problem, there's one thing that got on my nerves, which is the behavior of textbox cells in edit mode: It is much too easy to leave the edit mode accidentally, simply by pressing the cursor keys at the wrong time. For example when the text caret is positioned behind the last character of the textbox cell content, and you press the right arrow key

ListView DataPager with SQL-Statements that already return only paged data.



As far as I understand the ListView-Control and the DataPager-Control, they work like this: The ListView gets its datarows from its data source, then the DataPager causes the ListView to display only a subset of those rows.

So what, when I want to use custom databinding for the ListView, that *already* gets the certain subset from SQL-Server (for better performance). Can I and how can I use the DataPager then?

Thanx, S.

JQuery Treeview plugin is not working with Ajax page load in ie8


Hi there,

I developed a page where i m using jquery treeview plugin to generate tree. It worked fine on FF, IE8 with out ajax.

But when my page loads thro' ajax call in IE8, it is not collapsing or expanding tree.

Any idea?

Thanks in advance.

Run Button Behavior: Suggestion/Bug


Note: This could be a bug or considered as suggestion. I am not looking for solution.

1: I have ImageWatermark.cshtml open and currently in focus.

2: I have Photo1.jpg selected in left Files workspace.

3: Now if I Click "Run", instead of opening ImageWatermark.cshtml it trys to open Photo1.jpg and it fails.

So ideally I would expect the "open file (ImageWatermark.cshtml) to run rather than running the selection from Workspace.

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend