No selection brush when SelectedItem is bound

4 posts, 1 answers
  1. Owen
    Owen avatar
    7 posts
    Member since:
    Apr 2009

    Posted 15 Feb 2010 Link to this post

        public partial class MainWindow : Window 
        { 
            public MainWindow() 
            { 
                InitializeComponent(); 
                (Resources["DataSource"as RadTreeViewSampleData).SelectedItem = (radTreeView.ItemsSource as IEnumerable<League>).LastOrDefault(); 
            } 
        } 
        public class League : DependencyObject 
        { 
            public League(string name) 
            { 
                this.Name = name; 
                this.Leagues = new List<League>(); 
            } 
     
            public string Name 
            { 
                get
                set
            } 
     
            public List<League> Leagues 
            { 
                get
                set
            } 
        } 
     
        public class RadTreeViewSampleData : DependencyObject 
        { 
            /// <summary> 
            /// Identifies the SelectedItem dependency property. 
            /// </summary> 
            public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem"typeof(League), typeof(RadTreeViewSampleData)); 
     
            /// <summary> 
            /// Gets or sets SelectedItem.  This is a dependency property. 
            /// </summary> 
            public League SelectedItem 
            { 
                get { return (League)GetValue(SelectedItemProperty); } 
                set { SetValue(SelectedItemProperty, value); } 
            } 
     
            public RadTreeViewSampleData() 
            { 
                this.InitializeLeaguesDataSource(); 
            } 
     
            public List<League> LeaguesDataSource 
            { 
                get
                set
            } 
     
            private void InitializeLeaguesDataSource() 
            { 
                this.LeaguesDataSource = new List<League>(); 
                League l; 
                League d; 
                this.LeaguesDataSource.Add(l = new League("League A")); 
            } 
        } 
    <Window.Resources> 
            <local:RadTreeViewSampleData x:Key="DataSource"/> 
        </Window.Resources> 
        <telerik:RadTreeView x:Name="radTreeView" ItemsSource="{Binding Path=LeaguesDataSource, Source={StaticResource DataSource}}" SelectedItem="{Binding Path=SelectedItem, Source={StaticResource DataSource}, Mode=TwoWay}"
            <telerik:RadTreeView.ItemTemplate> 
                <HierarchicalDataTemplate ItemsSource="{Binding Path=Leagues}"
                    <TextBlock Text="{Binding Path=Name}" /> 
                </HierarchicalDataTemplate> 
            </telerik:RadTreeView.ItemTemplate> 
        </telerik:RadTreeView> 
    The SelectedItem is set properly but does not have the selection brush applied appropriately to its container.
  2. Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 16 Feb 2010 Link to this post

    Hi Owen,

    Please find attached your fixed sample code.
    I added an addition property IsSelected to the League class:
    public class League : DependencyObject
       {
           public League(string name, bool isSelected)
           {
               this.Name = name;
               this.Leagues = new List<League>();
               IsSelected = isSelected;
           }
     
           public string Name
           {
               get;
               set;
           }
     
           public List<League> Leagues
           {
               get;
               set;
           }
     
           public bool IsSelected
           {
               get;
               set;
           }
       }

    I also added a simple style binding for the selected state:
    <telerik:RadTreeView x:Name="radTreeView"  ItemsSource="{Binding Path=LeaguesDataSource, Source={StaticResource DataSource}}" SelectedItem="{Binding Path=SelectedItem, Source={StaticResource DataSource}, Mode=TwoWay}">
           <telerik:RadTreeView.ItemContainerStyle>
                   <Style TargetType="{x:Type telerik:RadTreeViewItem}">
                       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                   </Style>
           </telerik:RadTreeView.ItemContainerStyle>
           <telerik:RadTreeView.ItemTemplate>
               <HierarchicalDataTemplate ItemsSource="{Binding Path=Leagues}">
                   <TextBlock Text="{Binding Path=Name}" />
               </HierarchicalDataTemplate>
           </telerik:RadTreeView.ItemTemplate>
       </telerik:RadTreeView>

    Note you can handle the select behavior of RadTreeView only by handling IsSelected property.
    I hope that this will help you.
    Please let us know if you have any other questions or need some more help.

    Sincerely yours,
    Bobi
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Owen
    Owen avatar
    7 posts
    Member since:
    Apr 2009

    Posted 16 Feb 2010 Link to this post

    This being the case should the SelectedItem property on RadTreeView be readonly?
  5. Answer
    Bobi
    Admin
    Bobi avatar
    513 posts

    Posted 17 Feb 2010 Link to this post

    Hello Owen,

    SelectedItem is not read only but in the current released version it is not working correctly. However we will fix this for some of the next internal builds.
    For now you can use the workaround with IsSelected instead.

    Kind regards,
    Bobi
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top