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

No selection brush when SelectedItem is bound

3 Answers 106 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Owen
Top achievements
Rank 1
Owen asked on 15 Feb 2010, 08:48 PM
    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.

3 Answers, 1 is accepted

Sort by
0
Bobi
Telerik team
answered on 16 Feb 2010, 09:19 AM
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.
0
Owen
Top achievements
Rank 1
answered on 16 Feb 2010, 01:57 PM
This being the case should the SelectedItem property on RadTreeView be readonly?
0
Accepted
Bobi
Telerik team
answered on 17 Feb 2010, 01:27 PM
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.
Tags
TreeView
Asked by
Owen
Top achievements
Rank 1
Answers by
Bobi
Telerik team
Owen
Top achievements
Rank 1
Share this question
or