Treview won't display header while binding to ObservableCollection

8 posts, 1 answers
  1. Ruben
    Ruben avatar
    2 posts
    Member since:
    Mar 2009

    Posted 18 Aug 2009 Link to this post

    I am trying to bind a treeview with checkboxes to a observablecollection. It works for the checkstate but the header wont render. What am I doing wrong?

    The xaml:

    <UserControl.Resources> 
        <telerik:ContainerBindingCollection x:Key="TreeItemContainerBindings">             
                <telerik:ContainerBinding PropertyName="CheckState" Binding="{Binding Checked, Mode=TwoWay}" /> 
                <telerik:ContainerBinding PropertyName="Header" Binding="{Binding Name}" /> 
            </telerik:ContainerBindingCollection> 
    </UserControl.Resources> 
     
    <telerikNavigation:RadTreeView x:Name="TestTreeView" Margin="0,67,50,19" IsOptionElementsEnabled="True" 
                    IsTriStateMode="true" IsLineEnabled="True" SelectionMode="Single" IsEditable="false" 
                    IsDragDropEnabled="False" ItemsOptionListType="CheckList"   
                    RenderTransformOrigin="0.5,0.5" IsExpandOnDblClickEnabled="True" IsExpandOnSingleClickEnabled="False"   
                     HorizontalAlignment="Right" Width="282"  > 
                 
                <telerikNavigation:RadTreeView.ItemTemplate>    
                      
                    <telerik:HierarchicalDataTemplate ItemsSource="{Binding SubSRS}" 
                                                      telerik:ContainerBinding.ContainerBindings="{StaticResource TreeItemContainerBindings}">                     
                    </telerik:HierarchicalDataTemplate>                  
                </telerikNavigation:RadTreeView.ItemTemplate> 
     
            </telerikNavigation:RadTreeView> 

    And the code:
    1 public partial class MainPage : UserControl  
    2     {   
    3         ObservableCollection<SRS> SRSObservable = new ObservableCollection<SRS>();  
    4         public SRS srs = new SRS();  
    5         public MainPage()  
    6         {  
    7             InitializeComponent();  
    8             int[] srsArray = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 24, 25, 26, 84 };  
    9               
    10             srs.Checked = ToggleState.On;  
    11             srs.Name = "SRS";  
    12             srs.SubSRS = new ObservableCollection<SRS>();  
    13  
    14             foreach (var i in srsArray)  
    15             {                                 
    16                 SRS subsrs = new SRS();  
    17  
    18                 subsrs.Checked = ToggleState.Indeterminate;  
    19                 subsrs.Name = i.ToString();  
    20                 subsrs.Enabled = false;  
    21                 srs.SubSRS.Add(subsrs);  
    22             }  
    23             SRSObservable.Add(srs);  
    24             TestTreeView.ItemsSource = SRSObservable;  
    25        }  
    26 }  
    27  
    28 public class SRS  
    29     {  
    30         public ObservableCollection<SRS> SubSRS { getset; }  
    31  
    32         public string Name { getset; }  
    33  
    34         public bool Enabled { getset; }  
    35  
    36         public ToggleState Checked { getset; }  
    37  
    38     } 

    Thanks in advance.
    Ruben
  2. Answer
    Tihomir Petkov
    Admin
    Tihomir Petkov avatar
    576 posts

    Posted 20 Aug 2009 Link to this post

    Hello Ruben,

    You must set the Header property in the HierarchicalDataTemplate, and not in a  ContainerBinding:

    <

     

    telerik:ContainerBindingCollection x:Key="TreeItemContainerBindings">
    <telerik:ContainerBinding PropertyName="CheckState"
    Binding="{Binding Checked, Mode=TwoWay}" />
    </telerik:ContainerBindingCollection>

        and:

    <

     

    telerik:HierarchicalDataTemplate ItemsSource="{Binding SubSRS}"
    telerik:ContainerBinding.ContainerBindings="{StaticResource TreeItemContainerBindings}">
    <TextBox Text="{Binding Name}" />
    </telerik:HierarchicalDataTemplate>

    This small modification should make things work for you. Please let me know how it goes.

    Sincerely yours,
    Tihomir Petkov
    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.
  3. DevCraft banner
  4. Ruben
    Ruben avatar
    2 posts
    Member since:
    Mar 2009

    Posted 21 Aug 2009 Link to this post

    Thanks.
    Now it works. Its hard to be a noob. :)
  5. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 24 Aug 2009 Link to this post

    Just out of curiosity, why is it not possible to use a container binding to set the Header? This is something that I've tried to do as well, but couldn't get it working. Is there a reason why this particular property cannot be used in container bindings?

    /Henrik
  6. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 24 Aug 2009 Link to this post

    Hi Henrik,

    In PrepareContainerForItemOverride the item of the ItemControl is set as Content or Header of the Container (depending on the type of the containers). There we do not check whether a non-default value already exists and I guess we can do so.

    There are two issues that could arise from that though:
    - The container bindings are intended to mimick the StyleBindings in WPF and there the local value of the header will not be overriden by the style (I have not tested this).
    - At some places in the code it may be assumed that the item is ineed in the header / content. Again, I am not certain that this is the case.

    In short, we did not anticipate that the Header will be set in such a way and it could easily be fixed, but we have to be careful not to break any existing code.

    Best wishes,
    Miroslav
    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.
  7. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 25 Aug 2009 Link to this post

    If it is not too much trouble I think you should look into enabling it. For me, at least, it was unexpected that it did not work.
  8. Miroslav
    Admin
    Miroslav avatar
    922 posts

    Posted 26 Aug 2009 Link to this post

    Hello Henrik,

    Thanks for the feedback!

    I will look into this and if there are no side-effects, it will be fixed. I do not want to promise anything before I have tried it though.

    Greetings,
    Miroslav
    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.
  9. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 26 Aug 2009 Link to this post

    Great. Thanks.
Back to Top
DevCraft banner