.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

WPF Treeview - Binding to a collection with different depths, and styling differently

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

Apologies for the long post -- Read a few threads about this, but still find it hard to implement. Basically, I have a collection of objects, defined as:

public class LibData
  public string Name { get; set; }
  ObservableCollection<LibObject> _list;
  public ObservableCollection<LibObject> List { get { return _list; } }

  public LibData(string name, LibDataType type)
    this.Name = name;
    _list = new ObservableCollection<LibObject>();

View Complete Post

More Related Resource Links

Binding a TreeView to a collection of objects



I want to achieve this:


through data binding. That is, I've got this collection of objects of the same class (currently stored in a ArrayList), and I want to display them in a treeview, so that when you expand them you can see the values of their attributes.

Is it possible to do it via DataBinding?





Binding the sharepoint treeview to the quick launch data source

Is there any way i can bind the sharepoint treeview navigation to the same datasource as the quick launch bar? I'm trying to create a consistent navigation structure within a 4-site deep structure so the quick launch bar is rather clunky to use, but the treeview picks up on document libraries i don't want it to (i only want it to show subsites and pages).

Memory leak on binding to a WPF TreeView

We're having an issue with a TreeView hanging onto a viewmodel set that has been bound to it. We have a static treeview on the main window, and reset its ItemsSource to Nothing or a new viewmodel when new things need to be displayed. The problem seems to be that it's hanging onto the first item that it was bound to, even after the ItemsSource is set to nothing. The connection seems to be through some strange unknown binding that's in the EffectiveValues collection of the TreeView.   I was able to repro this in a test project, so I'll post the simplified code:   TreeView templates:   <Window.Resources> <HierarchicalDataTemplate DataType="{x:Type local:ParentViewModel}" ItemsSource="{Binding ChildVMs}"> <TextBlock Text="{Binding Name}"/> </HierarchicalDataTemplate> <DataTemplate DataType="{x:Type local:ChildViewModel}"> <TextBlock Text="{Binding Name}"/> </DataTemplate> </Window.Resources>   ViewModel definitions: (Shared Function to generate the itemssource for the treeview)   Public Class ParentViewModel Public Shared counter As Integer = 0 Public Shared Function GenerateViewModel() As List(Of ParentViewModel) Dim pvm As New ParentViewModel With {.Name = "Parent" & counter} ' Unique name pvm.ChildV

Binding to the TreeView control

My TreeView controls is rendering CheckBoxes on Leaf nodes. I want to bind to those nodes the ID of the underlying data item. I am not sure I can do this declaratively or if I need to hook up to the TreeNodeDataBound event. If the latter, how would I go about achieving this?   _hector

Binding DataGridView --- DataSet or Collection

This is my experiences. I can bind the DataGridView using DataSet and Collection. 1st expericence, If using DataSet, my DataGridView have an empty row at the last row (have to set AllowUserToAddRows Property to False). If using Collection, my DataGridView do not have empty row at the last row. 2nd experience, I know how to rebind the DataGridView using DataSet. But I dont know how to rebind the DataGridView using Collection. My collection as follow, private Collection<MasterAgentCompany> myDGV = new Collection<MasterAgentCompany>(); private BindingSource bindingSource = new BindingSource(); dgv1.DataSource = bindingSource; bindingSource.DataSource = myDGV; dgv1.Refresh(); /*THIS CODE IS OK ON 1ST TIME LOAD*/ /*IF I'M CALLING THIS CODE AGAIN, MY DATAGRIDVIEW NOT REFRESH THE NEW RECORD*/ I'm looking the right technique to rebind DataGridView using Collection I'm stuck. Really looking for help

WPF TreeView child nodes - collection is not a member of the first level nodes object



I was wondering if i could use a TreeView to display data from 2 separate collections where both collections are members of the same object. For example i have a view model that has 2 observable collections:



Each model has a property Brand which specifies the brand for each model. What i want to do is bind the first level to the Brands collection and the second level to the Models collection and specify a filter.

Thanks in advance.

Binding a treeview to a LinqToSQL class...


Hi all,

I have a LinQtoSQL class which encapsulates a table with the following relationship:

Id (int)
Parent ID(int)
Name (string)

Here, the parent ID is the id of another row int he same table. So, the table could look like:

1, null, grandpa
2,1, mom

What I want to do is the create a treeview that binds to the table so that it looks like this:


Also, I should be able to add or delete nodes/rows...

Any ideas on how to do this? Is this even possible without writing the code to do all the population and the edit etc?





Binding to an observable collection in a custom WPF control


Hi everyone,

I'll first explain a bit what I'm doing so that the code paste will make a bit more sense. I'm creating a circular menu control for Microsoft Surface (this question is far from Surface specific though, but otherwise this type of control wouldn't make sense :) ), it has some 'pieces' which are all buttons. I've created a 'Menu' control which contains the menu items, I made a custom control for these items (so I could add them to the menu within XAML), and I made a Button control which actually does the drawing of the path etc. I've done this in this specific way because the angle of the button's path will depend on the number of pieces within the menu. Anyway the above all works fine and dandy and I'm happy with the result. However now I'm trying to get all of this to work.

What I want to do is to use MVVM in my application and bind the MenuItems to a command in my ViewModel. So I have an AdminToolbox which is a view containing the menu and there's an AdminToolboxViewModel which is the ViewModel with the command and the items that I'd need to bind to my menu to create the menu items.

Some code pastes:



Binding a Treeview to an exception...

I'm not too familiar with Hierarchal DataTemplates yet, but here's what I want to do. 

I have a message box that has a small expander on the bottom with a treeview inside.  I want to hook the treeview up so that during runtime when an error occurs it can be caught, loaded into this treeview and displayed (including all the inner exceptions). 

How would I go about doing this? 

I'm fairly familiar with wpf and I can get everything into the message box correctly it's just the final display that I can't do yet. Also, I'm using VS 2010 Ultimate Beta 2.  Thanks in advance!!

Problem binding a collection to a view model on POST when manipulating the DOM with javascript.


Hi, i'm currently working on a strongly typed view and i'm binding a collection to a table like this:


for (int i = 0; i < Model.Cars.Count; i++)





<%= Html.Hidden(String.Format("Cars[{0}].Id", i),




<%= HtmlTextBox(String.Format("Cars[{0}].CarModel", i),



WPF TreeView Binding


Hello All,

I am having a heck of a time trying to get a TreeView control bounded to a List which is a member of Class which is a member of the MainWindow. Here is a contrived example similar to what I am trying today. Can anyone help me bind so that the Server Names show as Items in the TreeView. This would be a huge help.


Custom Classes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SystemsManagement
    public class ServerManager
        public List<Server> Servers { get; private set; }

        public ServerManager()
            Servers = new List<Server>();

            Servers.Add(new Server("Server1"));
            Servers.Add(new Server("Server2"));


    public class Server

TwoWay data binding to a specified index and property in a collection


Hello everybody,

my problem is, I can't bind to a specified item and property in a collection. The code:

    <test:ValueConverter x:Key="valueConverter" />
    <ContentPresenter x:Key="cont1" Content="{Binding Path=.[0], Mode=TwoWay}" />

Styling a complex HierarchicalDataTemplate for TreeView

I've got a (reasonably) complex hierarchy that I would like to display in a TreeView. In fact I can quite easily display the hierarchy in a TreeView - the problems start when I want to style those hierachy items.

Let me try to explain the data structure initially:

I have an "organisation" which contains collections of sub-"organisations" and "people".
"People" can contain collections of "items".
"Item" is a leaf node which does not contain any further children.

I make use of the excellent ComplexDataTemplates control library and my TreeView and it's associated HierarchicalDataTemplates is described thus:

      <!-- IEnumerable[OrganisationViewModel] -->
<HierarchicalDataTemplate x:Key="IEnumerable[OrganisationViewModel]" ItemsSource="{Binding Path=.}">
<TextBlock Text="Sub-units" />

<!-- OrganisationViewModel -->
<HierarchicalDataTemplate DataType="{x:Type vm:OrganisationViewModel}">

Dynamicobject collection binding on WPF DataGrid , not working ?



I am trying to bind dynamic list of items to the WPF DataGrid , but its not working .

my Dynamic list is

 // CustomDynamicObj is inherited from DynamicObject

  dynamic listItems = new List<CustomDynamicObj>();

 this.dataGrid1.DataContext = listItems;

And my XAML is

   <DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" Name="dataGrid1" VerticalAlignment="Stretch"  />

If I add columns through code in DataGrid then above code works fine , but I want to Autogenerate columns .

Any workaround for this plz ?



Nonstandard Treeview Binding


I am currently binding a Treeview to an Entity Framework objectset, which is working just fine. However I would like to organize the nodes in the treeview differently.

For example, if I bind so the root nodes represent the Students collection and the leaf nodes are that student's Subjects collection, that is just fine, but can I change the arrangement so that there are two root nodes, say Male and Female students, according to a property of the Student, and underneath those, the leaf nodes are say Bob, Dave, Steve under the Male root. And Jane, Sheila and Gertrude under the Female root?

In other words, instead of binding to Students and a collection belonging to Students, can I set up the treeview in such a way that the root nodes are manually specified, and the leaf nodes are under the correct root based on a property of the Students?

Thanks for any advice.

Binding a Collection to a ComboBox


The following is a the class, a list of which I wish to display in a WPF combo box
    public class DivisionItem

        private int _divn_ID = -1;
        private string _divn_Nm = "";
        private int _divn_Mgr = -1;

        private string _addrLn1 =&nb

Binding WPF Controls to an Entity Data Model

In this interview, programming writer, McLean Schofield, demonstrates how to bind WPF controls to an entity data model, using Visual Studio 2010 Beta 1. You can also learn more in the topic: Walkthrough: Binding WPF Controls to an Entity Data Model.
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