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 8054371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:07:53+00:00 2026-06-05T08:07:53+00:00

I have some complex requirement to create a DataTemplate for a TreeView in Silverlight.

  • 0

I have some complex requirement to create a DataTemplate for a TreeView in Silverlight. This is using MVVM design pattern.
here is what i want:

Package1
Child1
Child2
Package2
Child3
Child4

Code:

 Class Package
 {
   string _Name;
   ObservableCollection<Child> _Childs;
public ObservableCollection<Child> Childs{get{return _Childs; } set{_Childs=value;}}
public string PackageName{get{return _Name; } set{_Name=value;}}

  }

 class Child
 {
   string ChildName;
public bool IsEnabled{get;set;}
public string Id{get; set;}
  }

Using the above data contract i should create a tree view:
Package Name being TreeItem header value.
Each child item is a child box with Content is ChildName and The Enabled property of the checkbox is binded to IsEnabled of Child.

I have tried the following code but it is not showing any tree view at all.

 <sdk:TreeView x:Name="SelectedPackagesTV" ItemsSource="{Binding Packages, Mode=TwoWay}"
                              ItemTemplate="{StaticResource PackageTemplate}">
                    <sdk:TreeView.Resources>
                        <DataTemplate x:Key="FormsTemplate" >
                            <CheckBox Content="{Binding Id, Mode=TwoWay}" IsEnabled="{Binding IsEnabled}" >

                            </CheckBox>
                        </DataTemplate>
                        <sdk:HierarchicalDataTemplate x:Key="PackageTemplate" ItemsSource="{Binding Childs, Mode=TwoWay}" ItemTemplate="{StaticResource FormsTemplate}">
                            <TextBlock Text="{Binding Name, Mode=TwoWay}" />
                        </sdk:HierarchicalDataTemplate>                            
                    </sdk:TreeView.Resources>                        
                </sdk:TreeView>

NOTE: I am populating values to the PackagesCollection in the view model constructor. Will that be a problem?
Let me know if I am missing anything and also suggest me best way to resolve this.

Thanks in advance.

  • 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-06-05T08:07:54+00:00Added an answer on June 5, 2026 at 8:07 am

    Edit:

    The binding in the “PackageTemplate” is set to “Name” but the public property on the class is “PackageName”.

    Here is a sample of creating the data and attaching it. I have tested this and it works.

    public partial class MainWindow : Window
    {
    
        public MainWindow()
        {
            InitializeComponent();
    
            this.DataContext = new ViewModel();
        }
    
        public class Package
        {
            public string Name { get; set; }
            public ObservableCollection<Child> Childs { get; set; }
        }
    
        public class Child
        {
            public string ChildName { get; set; }
            public bool IsEnabled { get; set; }
        }
    
        public class ViewModel  : INotifyPropertyChanged
        {
            private ObservableCollection<Package> _packages;
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public string TestStr { get { return "testing123"; } }
    
            public ViewModel()
            {
                List<Package> list = new List<Package>() { new Package() { Name = "pkg1", Childs = new ObservableCollection<Child>(new List<Child>() { new Child() { ChildName = "Child1" } }) } };
                this.Packages = new ObservableCollection<Package>(list);
            }
    
            public ObservableCollection<Package> Packages
            {
                get
                {
                    return this._packages;
                }
                set
                {
                    this._packages = value;
                }
            }
    
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    }
    

    A few things:

    Your business objects should look like this:

    public class Package
        {
            public string Name { get; set; }
            public ObservableCollection<Child> Childs { get; set; }
        }
    
        public class Child
        {
            public string ChildName { get; set; }
            public bool IsEnabled { get; set; }
        }
    

    Binding can only work on public Properties, not private fields

    Your xaml can be corrected to:

    <sdk:TreeView ItemTemplate= "{StaticResoruce PackageTemplate}"  ItemsSource="{Binding Packages}">
    <sdk:TreeView.Resources>
        <sdk:HierarchicalDataTemplate x:Key="PackageTemplate" 
                                          ItemsSource="{Binding Childs,Mode=TwoWay}"
                                          ItemTemplate="{StaticResource ChildTemplate}">
            <TextBlock Text="{Binding Name,Mode=TwoWay}" />
        </sdk:HierarchicalDataTemplate>
    
        <sdk:DataTemplate x:Key="ChildTemplate" >
            <CheckBox  Content="{Binding ChildName,Mode=TwoWay}"
                           IsEnabled="{Binding IsEnabled}">
            </CheckBox>
        </sdk:DataTemplate>
    </sdk:TreeView.Resources>
    

    The templates need to be in an enclosing “Resources” bracket, either of the TreeView or the UserControl.

    Your PackageTemplate should have the ItemsSource “Childs” instead of Packages. You are binding the PackageTemplate to “PackageName” but the Package class states “Name”.

    Not an error but because your ChildTemplate has no children of its own it only needs to be a DataTemplate, no hierarchy.

    Also

    ItemTemplate= {"StaticResoruce PackageTemplate"}
    

    Should be:

    ItemTemplate= "{StaticResource PackageTemplate}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some complex types here so I decided to use nifty trick to
I'm using .NET 3.5. We have some complex third-party classes which are automatically generated
I have a MVVM application that contains multiple views with some complex IsReadOnly rules
I have code written in c, which contains some complex algorithms and I want
I have some what complex UI requirement, need help in understanding type of control/style/template
I think I have a complex requirement. It's a combinatorial permutation using Oracle 10.2,
I have a following requirement for a very complex UI. (Complex here means there
I have some complex regular expressions which I need to comment for readability and
I have some idea that it is due to some complex calculation, but i
I have a list of objects, that are pre-sorted based on some complex criteria

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.