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

RadTreeView SelectedItem Binding

11 Answers 753 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Alexey
Top achievements
Rank 2
Alexey asked on 10 Jun 2009, 10:51 AM
Hi,

Why SelectedItem is readonly? (Standard ListBox and ComboBox SelectedItem are not readonly) Because of it we cannot bind it to our Model. Is there any other property which can be used for two way databinding of selected item.

Thanks,
Alexey Zakharov.

11 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 11 Jun 2009, 07:56 AM
Hi Alexey,

This is the pattern for the SL and WPF treeviews.

You can use the IsSelected property of the RadTreeViewItem to do the two-way binding.

Sincerely yours,
Valentin.Stoychev
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
Alexey
Top achievements
Rank 2
answered on 11 Jun 2009, 08:10 AM
Hi Valentin,

Could you please provide a xaml code sample where you are binding selected item via IsSelected property.

Thanks,
Alexey Zakharov.
0
Bobi
Telerik team
answered on 11 Jun 2009, 12:33 PM
Hello Alexey,

Please find attached a simple project that shows how to bind IsSelected property of RadTreeViewItem.

In the given example I did the following:

1. Implementation of INotifyPropertyChanges

public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

2. Added a custom property of type bool:

private bool select;   
public bool Select
        {
            get { return this.select; }

            set
            {
                if (value != this.select)
                {
                    this.select = value;
                    NotifyPropertyChanged("Select");
                }
            }

        }

3. Added a TwoWay Binding in XAML:

<telerik:RadTreeViewItem Header="2" IsSelected="{Binding Select, Mode=TwoWay}"/>

4.Added DataContext and default value for the newly created property:

public Page()
        {
            InitializeComponent();
            this.treeView.DataContext = this;
            this.Select = true;
        }

I hope that this answers your question.

Regards,
Boryana
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
Alexey
Top achievements
Rank 2
answered on 11 Jun 2009, 02:26 PM
Thank you Boryana.

I've already know all basics of databinding ;-)

We are not declare treeview items with xaml.

All treeview items are generated automatically after supplying of itemssource.

That is why I have no treeview items in xaml and cannot understand how I can bind its IsSelect property.

Best regards,
Alexey Zakharov.
0
Bobi
Telerik team
answered on 11 Jun 2009, 03:20 PM
Hello Alexey,

Please find attached a sample project that shows you how to use the IsSelected property of  RadTreeViewItem to do the two-way binding when having RadTreeView which items are generated automatically after supplying of ItemsSource.

All you have to do is just to set ItemContainerStyleproperty:

       <telerik:RadTreeView.ItemContainerStyle>
                <Style TargetType="telerik:RadTreeViewItem">
                    <Setter Property="IsSelected" Value="{Binding Path=Select, Mode=TwoWay}" />
                </Style >
            </telerik:RadTreeView.ItemContainerStyle>
        </telerik:RadTreeView>


I hope that this answers your question.

Sincerely yours,
Boryana
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
Alexey
Top achievements
Rank 2
answered on 11 Jun 2009, 03:33 PM
Oh! =) Never thought about using style for databinding! Thanks a lot. I think it will also solve some other problems which I faced before.
0
Bobi
Telerik team
answered on 12 Jun 2009, 06:25 AM
Hello Alexey,

Please find attached a Silverlight project that demonstrates how to bind IsSelected property of RadTreeView to a custom value.

You have to do the following:

1.Add ContainerBindingCollection in the Resources :

        <core:ContainerBindingCollection x:Name="SelectedItemsCollection">
            <core:ContainerBinding PropertyName="IsSelected" Binding="{Binding Select,  Mode=TwoWay}" />
        </core:ContainerBindingCollection>

2.Set the ContainerBinding  property of the HierarchicalDataTemplate:

<core:HierarchicalDataTemplate  x:key = "areaTemplate "core:ContainerBinding.ContainerBindings="{StaticResource SelectedItemsCollection}"  ..../>

3. Finaly set RadTreeView ItemTemplate and ItemsSource

<telerik:RadTreeView x:Name="tree" SelectionMode="Multiple"
                ItemsSource="{Binding Source={StaticResource TreeAreaDS}}"
                ItemTemplate="{StaticResource areaTemplate}">
        </telerik:RadTreeView>

I hope that this will help ypu.

Regards,
Boryana
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
efi
Top achievements
Rank 1
answered on 21 Oct 2009, 01:30 PM
Hello,

I need this solution due to NOT having SelectedItem support with Bindings (FOR SILVERLIGHT).
But, the solution suggested here regarding this line: "ItemsSource="{Binding Source={StaticResource TreeAreaDS}}"
isn't good for me because I need an ObservableCollection to update the collection online all the time from C# !
So I put inside ItemSource="{Binding TreeAreaObservableCollection, Mode=TwoWay, UpdateSourceTrigger=Default}"
And in runtime I get NullReferenceException ("Object reference not set to an instance of an object.")
even though my objects are all OK (Not Null !)


What can I do to get it work ?

Thanks,
Efi.
0
Bobi
Telerik team
answered on 22 Oct 2009, 09:00 AM
Hi efi,

Please explain in more detail whet version of telerik assemblies are you using?
Can you please send us a sample project in order to fix it or explain in more details the structure of your application?

Sincerely yours,
Boryana
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
efi
Top achievements
Rank 1
answered on 22 Oct 2009, 12:01 PM
Hi Boryana,

1) I'm using Silverlight 3.0 with Telerik: RadControls_for_Silverlight_2009_2_1009

2) I cannot send you a sample project. But, please look at your sample project you supplied on Jun 12 to "Alexey",

    As I mentioned, I need the exact thing working with Binding to an ObservableCollection and NOT "StaticResource TreeAreaDS" 
    as shown in your example because I need to update that collection all the time from C# (CodeBehind) (This is my responsibility) !

* All I'm asking is to accomplish that kind of binding (as mentioned in my last Post/Reply) which results with an error !

Thanks,
EFI.
0
Bobi
Telerik team
answered on 27 Oct 2009, 06:54 AM
Hello efi,

We were unable to reproduce the issue with NullReferenceException.
Please find attached the sample project you mentioned but with some fixes. I added some Edit functionality to the treeview and TreeAreaDS is already an ObservableCollection<Organization>.
I am not sure if I understand you correctly what exactly are you trying to achieve, so please take a look at the sample project and if it is not enough please let us know and explain what exactly are you looking for so that will be able to send you a solution to your problem. If possible send us a simple structure of your application or some sample code.

All the best,
Boryana
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
Alexey
Top achievements
Rank 2
Answers by
Valentin.Stoychev
Telerik team
Alexey
Top achievements
Rank 2
Bobi
Telerik team
efi
Top achievements
Rank 1
Share this question
or