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

Treview won't display header while binding to ObservableCollection

7 Answers 112 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ruben
Top achievements
Rank 1
Ruben asked on 18 Aug 2009, 12:38 PM

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:
public partial class MainPage : UserControl  
    {   
        ObservableCollection<SRS> SRSObservable = new ObservableCollection<SRS>();  
        public SRS srs = new SRS();  
        public MainPage()  
        {  
            InitializeComponent();  
            int[] srsArray = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 24, 25, 26, 84 };  
              
            srs.Checked = ToggleState.On;  
            srs.Name = "SRS";  
            srs.SubSRS = new ObservableCollection<SRS>();  
 
            foreach (var i in srsArray)  
            {                                 
                SRS subsrs = new SRS();  
 
                subsrs.Checked = ToggleState.Indeterminate;  
                subsrs.Name = i.ToString();  
                subsrs.Enabled = false;  
                srs.SubSRS.Add(subsrs);  
            }  
            SRSObservable.Add(srs);  
            TestTreeView.ItemsSource = SRSObservable;  
       }  
}  
 
public class SRS  
    {  
        public ObservableCollection<SRS> SubSRS { getset; }  
 
        public string Name { getset; }  
 
        public bool Enabled { getset; }  
 
        public ToggleState Checked { getset; }  
 
    } 

Thanks in advance.
Ruben

7 Answers, 1 is accepted

Sort by
0
Accepted
Tihomir Petkov
Telerik team
answered on 20 Aug 2009, 02:46 PM
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.
0
Ruben
Top achievements
Rank 1
answered on 21 Aug 2009, 07:40 AM
Thanks.
Now it works. Its hard to be a noob. :)
0
hwsoderlund
Top achievements
Rank 1
answered on 24 Aug 2009, 07:43 AM
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
0
Miroslav
Telerik team
answered on 24 Aug 2009, 08:41 AM
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.
0
hwsoderlund
Top achievements
Rank 1
answered on 25 Aug 2009, 06:57 AM
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.
0
Miroslav
Telerik team
answered on 26 Aug 2009, 08:58 AM
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.
0
hwsoderlund
Top achievements
Rank 1
answered on 26 Aug 2009, 09:22 AM
Great. Thanks.
Tags
TreeView
Asked by
Ruben
Top achievements
Rank 1
Answers by
Tihomir Petkov
Telerik team
Ruben
Top achievements
Rank 1
hwsoderlund
Top achievements
Rank 1
Miroslav
Telerik team
Share this question
or