This is a migrated thread and some comments may be shown as answers.

Binding the Checked value in the TreeView

4 Answers 153 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
T.Y.
Top achievements
Rank 1
T.Y. asked on 27 Apr 2009, 05:56 AM
Hi

I tried to follow you example with the DataItem class. I add a couple of properties (in order to make the DataItem class relevant to my application) and I built the tree just as in your samples.

Now I'm trying to bind a value from the DataItem class ( a boolean property) to the IsChecked value of the CheckBox in each of the elements in the tree.
I'm using a DataTemplate with a CheckBox  and a TextBlock. Here is the template definition:

public class DataItem
{
   ConfigSectiob m_element;
   public ConfigSection Element
   {
      get{return m_element;}
      set{m_element = value; }
  }
     public string Name {get; ,set;}
     public bool Checked {get; ,set;}
     public List<DataItem> Items {get; ,set;}    

   public DataItem()
   { 
     this.Items = new  List<DataItem>()
   }  
}

 

My problem is that the binding proccess does not work and I get error at runtime. I attached the DataItem class code too.

 private void InstantiateTemplates()  
        {  
            string s1 = @"
                <DataTemplate x:Name=""LayoutRoot""
                    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""  
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""  
                    xmlns:telerik=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls""  
                    xmlns:telerikNavigation=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"">  
                    <CheckBox IsChecked=""{Binding Checked}"" />  
                    <TextBlock Text=""{Binding Names}""/>                    
                </DataTemplate>  
            ";  
            string s2 = @"
                <telerik:HierarchicalDataTemplate ItemsSource=""{Binding Items}""
                    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""  
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""  
                    xmlns:telerik=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls""  
                    xmlns:telerikNavigation=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"">                   
                    <CheckBox IsChecked=""{Binding Checked}"" />    
                    <TextBlock Text=""{Binding Names}""/>  
                </telerik:HierarchicalDataTemplate>  
            ";  
            string s3 = @"
                <telerik:HierarchicalDataTemplate ItemsSource=""{Binding Items}""
                    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""  
                    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""  
                    xmlns:telerik=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls""  
                    xmlns:telerikNavigation=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"">                   
                    <CheckBox IsChecked=""{Binding Checked}"" />  
                    <TextBlock Text=""{Binding Names}"" />  
                </telerik:HierarchicalDataTemplate>  
            ";  
 
            this.dataTemplate2 = (DataTemplate)XamlReader.Load(s1);  
           
           
            this.subItemTemplate2 = (HierarchicalDataTemplate)XamlReader.Load(s2);  
            this.subItemTemplate2.ItemTemplate = this.dataTemplate2;  
 
            this.itemTemplate2 = (HierarchicalDataTemplate)XamlReader.Load(s3);  
            this.itemTemplate2.ItemTemplate = this.subItemTemplate2;  
        }  
 
  List<DataItem> items;  
 
        public List<DataItem> Items  
        {  
            get { return items; }  
            set { items = value; }  
        }  
public Page()
        {  
            InitializeComponent();  
 

items =

new List<DataItem>();

 

 

this.InstantiateTemplates();

 

            telerikTreeView.ItemsSource = Items;  
                    telerikTreeView.ItemTemplate = this.itemTemplate2;  
                    telerikTreeView.ItemsOptionListType = OptionListType.CheckList;  
                    telerikTreeView.IsOptionElementsEnabled = true;  
   }  
 
   

Do you have any idea?
Thanks

4 Answers, 1 is accepted

Sort by
0
hwsoderlund
Top achievements
Rank 1
answered on 27 Apr 2009, 09:31 AM
I haven't looked at your example code in detail, but for data binding to work you must implement the interface INotifyPropertyChanged on the DataItem class.
0
Bobi
Telerik team
answered on 27 Apr 2009, 03:08 PM
Hello Henrik,

Your DataItem class has to implement the  INotifyPropertyChanged  interface in order to be able to change the value of the desired property. Your code should looks like this:

 private string checked;
 public string Checked
    { 
        get
        { 
            return this.checked; 
        } 
        set
        { 
            if (this.checked!= value) 
            { 
                this.checked= value; 
                OnPropertyChanged("Checked "); 
            } 
        } 
    }

protected virtual void OnPropertyChanged(String propertyName) 
    { 
        if (String.IsNullOrEmpty(propertyName)) 
        { 
            return; 
        } 
        if (PropertyChanged != null) 
        { 
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
        } 
    } 
 
  
    public event PropertyChangedEventHandler PropertyChanged;

As for the DataTemplates, please take a look at the following article:
http://msdn.microsoft.com/en-us/library/system.windows.hierarchicaldatatemplate.aspx

Let me know if this helps.

All the best,
Boryana
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
T.Y.
Top achievements
Rank 1
answered on 28 Apr 2009, 08:38 AM
Hi

I tried to use the INotifyPropertyChange interface but my problem is something with the Binding definition in my applicaiton.
At the start of the application I read some XML configuration that provide me with the information about the tree nodes that should be checked. I want to set the binding so that when I set the value of the property "Checked" of my DataItem class, the tree will check that node too by using binding.
But...

It means that something is not working with the binding.

Can you help me? Maybe you can provide a code sample that demonstrate this process?

Thanks,

Toybe
0
hwsoderlund
Top achievements
Rank 1
answered on 28 Apr 2009, 10:41 AM
Take a look at this:
http://cid-cc43a90a79374ebd.skydrive.live.com/self.aspx/Public/SilverlightApplication19.zip
Tags
TreeView
Asked by
T.Y.
Top achievements
Rank 1
Answers by
hwsoderlund
Top achievements
Rank 1
Bobi
Telerik team
T.Y.
Top achievements
Rank 1
Share this question
or