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

Posted by: the telerik blogs, on 27 Aug 2009 | View original | Bookmarked: 0 time(s)

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:

TreeViewInComboBox.zip

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>

 

SelfReferenceHierarchyTreeView.zip

 

I hope this helps.

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: C# | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 5497 | Hits: 23

Similar Posts

  • Telerik Launches RadControls for Silverlight 3 for Line-of-Business Application Development more
  • Why Embedded Silverlight Makes Sense more
  • Telerik Announces Support for Microsoft Silverlight 3 more
  • Moonlight 1.0 Released, Silverlight script updated – and a Chrome hack more
  • TreeView in a ComboBox dropdown using RadControls for Silverlight more
  • "Silverlight: Write and Win!" Contest more
  • RadControls for Silverlight and WPF more
  • TreeView and Web Services more
  • Why you should learn WPF (and how I can help) more
  • Silverlight Adventures more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD