Binding the Checked value in the TreeView

5 posts, 0 answers
  1. T.Y.
    T.Y. avatar
    17 posts
    Member since:
    Apr 2009

    Posted 27 Apr 2009 Link to this post

    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.

    1  private void InstantiateTemplates()  
    2         {  
    3             string s1 = @"
    4                 <DataTemplate x:Name=""LayoutRoot""
    5                     xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""  
    6                     xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""  
    7                     xmlns:telerik=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls""  
    8                     xmlns:telerikNavigation=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"">  
    9                     <CheckBox IsChecked=""{Binding Checked}"" />  
    10                     <TextBlock Text=""{Binding Names}""/>                    
    11                 </DataTemplate>  
    12             ";  
    13             string s2 = @"
    14                 <telerik:HierarchicalDataTemplate ItemsSource=""{Binding Items}""
    15                     xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""  
    16                     xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""  
    17                     xmlns:telerik=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls""  
    18                     xmlns:telerikNavigation=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"">                   
    19                     <CheckBox IsChecked=""{Binding Checked}"" />    
    20                     <TextBlock Text=""{Binding Names}""/>  
    21                 </telerik:HierarchicalDataTemplate>  
    22             ";  
    23             string s3 = @"
    24                 <telerik:HierarchicalDataTemplate ItemsSource=""{Binding Items}""
    25                     xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""  
    26                     xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""  
    27                     xmlns:telerik=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls""  
    28                     xmlns:telerikNavigation=""clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"">                   
    29                     <CheckBox IsChecked=""{Binding Checked}"" />  
    30                     <TextBlock Text=""{Binding Names}"" />  
    31                 </telerik:HierarchicalDataTemplate>  
    32             ";  
    33  
    34             this.dataTemplate2 = (DataTemplate)XamlReader.Load(s1);  
    35            
    36            
    37             this.subItemTemplate2 = (HierarchicalDataTemplate)XamlReader.Load(s2);  
    38             this.subItemTemplate2.ItemTemplate = this.dataTemplate2;  
    39  
    40             this.itemTemplate2 = (HierarchicalDataTemplate)XamlReader.Load(s3);  
    41             this.itemTemplate2.ItemTemplate = this.subItemTemplate2;  
    42         }  
    43  
    44   List<DataItem> items;  
    45  
    46         public List<DataItem> Items  
    47         {  
    48             get { return items; }  
    49             set { items = value; }  
    50         }  
    51

    public Page()
    52         {  
    53             InitializeComponent();  
    54  

    items =

    new List<DataItem>();

     

     

    this.InstantiateTemplates();

     

    55             telerikTreeView.ItemsSource = Items;  
    56                     telerikTreeView.ItemTemplate = this.itemTemplate2;  
    57                     telerikTreeView.ItemsOptionListType = OptionListType.CheckList;  
    58                     telerikTreeView.IsOptionElementsEnabled = true;  
       }  
    59  
    60    

    Do you have any idea?
    Thanks

  2. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 27 Apr 2009 Link to this post

    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.
  3. DevCraft banner
  4. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 27 Apr 2009 Link to this post

    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.
  5. T.Y.
    T.Y. avatar
    17 posts
    Member since:
    Apr 2009

    Posted 28 Apr 2009 Link to this post

    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
  6. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 28 Apr 2009 Link to this post

    Take a look at this:
    http://cid-cc43a90a79374ebd.skydrive.live.com/self.aspx/Public/SilverlightApplication19.zip
Back to Top
DevCraft banner