RadTreeView SelectedItem Binding

12 posts, 0 answers
  1. Alexey
    Alexey avatar
    79 posts
    Member since:
    Oct 2008

    Posted 10 Jun 2009 Link to this post

    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.
  2. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 11 Jun 2009 Link to this post

    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.
  3. DevCraft banner
  4. Alexey
    Alexey avatar
    79 posts
    Member since:
    Oct 2008

    Posted 11 Jun 2009 Link to this post

    Hi Valentin,

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

    Thanks,
    Alexey Zakharov.
  5. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 11 Jun 2009 Link to this post

    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.
  6. Alexey
    Alexey avatar
    79 posts
    Member since:
    Oct 2008

    Posted 11 Jun 2009 Link to this post

    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.
  7. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 11 Jun 2009 Link to this post

    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.
  8. Alexey
    Alexey avatar
    79 posts
    Member since:
    Oct 2008

    Posted 11 Jun 2009 Link to this post

    Oh! =) Never thought about using style for databinding! Thanks a lot. I think it will also solve some other problems which I faced before.
  9. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 12 Jun 2009 Link to this post

    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.
  10. efi
    efi avatar
    6 posts
    Member since:
    Sep 2008

    Posted 21 Oct 2009 Link to this post

    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.
  11. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 22 Oct 2009 Link to this post

    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.
  12. efi
    efi avatar
    6 posts
    Member since:
    Sep 2008

    Posted 22 Oct 2009 Link to this post

    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.
  13. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 27 Oct 2009 Link to this post

    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.
Back to Top
DevCraft banner