Telerik blogs

It seems that there are many developers that want to use this combo, as my previous blog posts are quite popular. The problem with the suggested solution is that you need to customize the control template of RadComboBox. There is a much easier way to put a RadTreeView in a Popup – by using a RadDropDownButton:

<telerik:RadDropDownButton HorizontalContentAlignment="Left" DropDownWidth="200" IsOpen="{Binding SelectedItem, Converter={StaticResource ObjectToFalseConverter}, ElementName=TreeView, Mode=TwoWay}" Content="{Binding SelectedItem.Text, FallbackValue='Please, select an item.', ElementName=TreeView}"> <telerik:RadDropDownButton.DropDownContent> <telerik:RadTreeView x:Name="TreeView" ItemsSource="{StaticResource Items}" ItemTemplate="{StaticResource ItemTemplate}" /> </telerik:RadDropDownButton.DropDownContent> </telerik:RadDropDownButton>

 

This way you could easily access the RadTreeView instance in the code-behind, however I would recommend using a ViewModel and bindings instead.

 

The DropDownButton Content property is bound to the SelectedItem property of RadTreeView. I used FallbackValue to show some text when there is no selection.

 

To close the DropDownButton I created a simple IValueConverter hack:

public class ObjectToFalseConverter : IValueConverter
{
    WeakReference oldValue = new WeakReference(null);

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != oldValue.Target || oldValue.Target == null)
        {
            oldValue.Target = value;
            return false;
        }
        return true;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return oldValue.Target;
    }
}

 

The converter returns false (in order to close the dropdown) when the saved old property value is different than the new one, otherwise true (to leave the button open). The reverse conversion always returns the saved old value. Note that the converter is a potential memory leak, so use it on your own risk :) If you want to avoid the converter hack, you will have to handle the SelectionChanged event of RadTreeView to close the dropdown.

 

I didn’t add support for initial selection in the RadTreeView, you could check the following online help article for more information:

http://www.telerik.com/help/silverlight/radtreeview-how-to-bind-selected-item.html

 

The full source code is available here:

 

I hope this helps.


About the Author

Valeri Hristov

Team Lead,
Platform Team

Comments

Comments are disabled in preview mode.