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

selecteditem

5 Answers 135 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Marcus Lambert
Top achievements
Rank 1
Marcus Lambert asked on 11 Sep 2009, 06:18 PM

Hi,

Am having a little trouble getting my head round the SelectedItem in the treeview control.

I use a ViewModel that has a obserabelcollection with hierarchical collection in it.

It displays fine using a HierarchicalDataTemplate.

On a MS Treeview I would also bind the SelectedItem back to my ViewModels SelectedNode property,I get an error if it do with yours ro do with the Selected Item being readonly as you allow for multipul selections. So what is the best way to get the selected Item in this situation using a ViewModel.


Thanks

5 Answers, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 17 Sep 2009, 07:34 AM
Hi Marcus,

Currently you cannot bind to RadTreeView's SelectedItem property. However, for next week's internal build, this functionality will be available.

In the meantime, please find attached a sample project that demonstrates you how to bind SelectedItem property when using a ViewModel.
For more information please visit the following blog posts:


http://geekswithblogs.net/lbugnion/archive/2009/02/18/simulating-issynchronizedwithcurrentitem-in-silverlight-part-2.aspx

In order to accomplish SelectedItem binding you have to add some additional extensions:
1. Add some extensions classes to your application as shown in the sample application.

2. Add an additional attached property:

public static readonly DependencyProperty IsSynchronizedWithCurrentItemProperty = 
    DependencyProperty.RegisterAttached("IsSynchronizedWithCurrentItem"typeof(bool), typeof(SelectorExtension), new PropertyMetadata(false
    (s, e) => 
 
    { 
 
        var control = s as RadTreeView; 
         
        if (control == null
        { 
 
            return
 
        } 
 
         
        if (control.ItemsSource != null
 
        { 
            if ((bool)e.NewValue) 
 
            { 
                SetBinding(control, control.ItemsSource as IObservableCollectionEx); 
            } 
            return
        } 
 
        if ((bool)e.NewValue) 
        { 
            control.SelectionChanged += new Telerik.Windows.Controls.SelectionChangedEventHandler(control_SelectionChanged); 
            _selectors.Add(control); 
        } 
        else 
        { 
            if (_selectors.Contains(control)) 
            { 
                _selectors.Remove(control); 
            } 
        } 
    })); 

3. Subscribe to SelectionChanged :

control.SelectionChanged += new SelectionChangedEventHandler(control_SelectionChanged); 

4. Add the desired code in the SelectionChanged  handler:

static void control_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e) 
{  
    ... 
 
    //Add the desired functionality here 
    SetBinding(control, source.SelectedObject); 
 
        ... 
 
private static void SetBinding(RadTreeView control, object source) 
    if (control.ItemContainerGenerator.ContainerFromItem(source) != null
    { 
        (control.ItemContainerGenerator.ContainerFromItem(source) as RadTreeViewItem).IsSelected = true
    } 

5. After you have done all explained in the previous steps set the attached property in XAML:

<telerik:RadTreeView  
    ItemTemplate="{StaticResource DataTemplate1}" 
    ItemsSource="{Binding Items}" 
    local:SelectorExtension.IsSynchronizedWithCurrentItem="True" /> 

Give the example a try and let me know how it works for you.

All the best,
Kiril Stanoev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Marcus Lambert
Top achievements
Rank 1
answered on 23 Oct 2009, 10:07 PM
Did this make the q3 beta ?
Marcus
0
Valentin.Stoychev
Telerik team
answered on 24 Oct 2009, 08:29 AM
Hi Marcus Lambert,

Yes - it is also available in the current Beta.

Regards,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Marcus Lambert
Top achievements
Rank 1
answered on 24 Oct 2009, 11:00 AM
Thanks for lettng us know.

I must be using a different build, or my references are wrong as I cant seem to bind to it in "TwoWay" mode.
0
Valentin.Stoychev
Telerik team
answered on 26 Oct 2009, 06:35 AM
Hi Marcus Lambert,

What assembly version are you using? Can you get the latest and let us know if the problem persist.

Regards,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
Marcus Lambert
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Marcus Lambert
Top achievements
Rank 1
Valentin.Stoychev
Telerik team
Share this question
or