TreeView in ComboBox, take 3 (Silverlight 3, WPF and RadControls)

by XAML Team | Comments 14

In my previous article on the same topic I described how to replace the ItemsPresenter of RadComboBox with a RadTreeView. It was as simple as updating the control template. Unfortunately the controls’ selection synchronization relied on a bug in RadComboBox that we recently fixed – the control was able to have a value in its SelectedItem property, that was not present in its Items collection. Now, in order to workaround this new problem, we have to either inherit RadComboBox, or create an attached behavior that will allow us to get the SelectedItem of the inner RadTreeView. The code you have to write is virtually the same in Silverlight and WPF:

<UserControl.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/TreeViewInComboBox;component/RadComboBoxStyle.xaml" /> </ResourceDictionary.MergedDictionaries> <telerik:HierarchicalDataTemplate x:Key="ItemTemplate" ItemsSource="{Binding Children}"> <TextBlock Text="{Binding Text}" /> </telerik:HierarchicalDataTemplate> </ResourceDictionary> </UserControl.Resources> <StackPanel x:Name="LayoutRoot"> <local:TreeViewCombo x:Name="treeViewCombo" ItemsSource="{Binding}" ItemTemplate="{StaticResource ItemTemplate}" Style="{StaticResource TreeViewInComboBox}" /> <TextBlock Text="{Binding ElementName=treeViewCombo, Path=SelectedTreeViewItem.Text}" /> </StackPanel>

 

The important parts from the new RadComboBoxStyle.xaml are the ContentPresenter and RadTreeView declarations in the RadComboBox EditableTemplate:

<ContentPresenter x:Name="ContentPresenterElement" IsHitTestVisible="False" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{Binding SelectedValue, ElementName=treeView}" />

<telerikNavigation:RadTreeView x:Name="treeView" ItemTemplate="{TemplateBinding ItemTemplate}" ItemsSource="{TemplateBinding ItemsSource}" DisplayMemberPath="{TemplateBinding DisplayMemberPath}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />

For more information, check the attached solution that contains 4 projects – two for Silverlight and two for WPF, demonstrating both attached behavior and inherited combo box:

As a bonus I am also attaching one more application, demonstrating how to mix the “old” approach for the same problem, with the self-referencing treeview example from yesterday – in this case, the Items collection of RadComboBox contains all treeview items and there is no problem to bind the RadTreeView SelectedValue property with RadComboBox SelectedItem. In this case we only need to update the RadTreeView declaration in RadComboBoxStyle.xaml – no inheritance, no attached properties, just small XAML change and everything works:

<ScrollViewer x:Name="PART_ScrollViewer" BorderThickness="0" VerticalScrollBarVisibility="Auto" telerik:StyleManager.Theme="{StaticResource Theme}"> <ScrollViewer.Resources> <local:HierarchyConverter x:Key="HierarchyConverter" /> </ScrollViewer.Resources> <!--It is important not to touch the SelectedValuePath property on this RadTreeView --> <telerikNavigation:RadTreeView x:Name="treeView" ItemTemplate="{TemplateBinding ItemTemplate}" ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource HierarchyConverter}}" DisplayMemberPath="{TemplateBinding DisplayMemberPath}" ScrollViewer.VerticalScrollBarVisibility="Disabled" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectedValue="{Binding SelectedItem, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" /> </ScrollViewer>

 

 

I hope this helps.

Posted in: howto combobox treeview

14 Comments

Dez
Hi Valeri,

Thanks for the updates of these TreeViewCombo. They are great soluations and are exactly what I am looking for.

However, I wonder how I can manually to select an item as default in my own initialization process.

Right now, I am using the approach of "inherited combo box". The only property get changed is SelectedTreeViewItem. But it will not be shown in the ComboBox until the drop down popup is open at least once.

Any suggestion?
Dez
Hi Valeri,

Do you also have a self-ReferenceHierarchy Sample for WPF? The ComboBox's ControlTemple in your siliverlight version, cannot be used in WPF, for it doesn't like the Treeview's SelectValue from templatedparent.
ramil
hello hristov,

i downloaded the sample from the link above.got it working! i just want to ask about he hard coded theme (office black). i removed it but somehow, the ScrollViewer wont get my application's theme. but the PickerToggleButton can.sorry if out of topic.
ramil
hello hristov,

i downloaded the sample from the link above.got it working! i just want to ask about he hard coded theme (office black). i removed it but somehow, the ScrollViewer wont get my application's theme. but the PickerToggleButton can.sorry if out of topic.
Ganesh Shivshankar
I am using the Self referencing heirarchy tree view in the RadCombo. It works just fine, but has a slight problem when placed in Child window. when the child window loads the first time the radcombo is populated perfectly. The 2nd time onwards it seems to be calling the HierarchyConverter directly even before populating its ItemsSource. This is causing the combobox to become empty.

Can you tell me if you faced this issue? I need a work around for this pls !
Raja. N
Hi,
I worked your treeviewcombobox, its working fantastic. I am facing a little problem in your example, How could I clear the selected value in treeviewcombobox without unbinding the data.

Thanks in advance
CesneK
Today, this example with current RadControls (2011.2.0920.40 WPF) not working.
Is there updated sample?
Or native and fully suppored RadTreeListCombo?
RadLover
Really guys, I donot know why you keep online demos without even checking whether they are working or not. It is giving me all schema not found and Invalid XAML. It's just waste of time for people like me who have daily deadlines...
telerik user
I am using your sample,
TreeViewInComboBox.zip, but once I select an item from the combo box, I am unable to click anywhere on the window untill i switch the window(Alt+tab) or again click on the combo box.
telerik user
I am using your sample,
TreeViewInComboBox.zip, but once I select an item from the combo box, I am unable to click anywhere on the window untill i switch the window(Alt+tab) or again click on the combo box.
telerik user
I am using your sample,
TreeViewInComboBox.zip, but once I select an item from the combo box, I am unable to click anywhere on the window untill i switch the window(Alt+tab) or again click on the combo box.
telerik user
I am using your sample,
TreeViewInComboBox.zip, but once I select an item from the combo box, I am unable to click anywhere on the window untill i switch the window(Alt+tab) or again click on the combo box.
telerik user
I am using your sample,
TreeViewInComboBox.zip, but once I select an item from the combo box, I am unable to click anywhere on the window untill i switch the window(Alt+tab) or again click on the combo box.

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by XAML Team - or - read latest articles in Developer Tools