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

How To:auto select the first item in radcombobox when use collectionViewSource?

2 Answers 219 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
lee hom
Top achievements
Rank 1
lee hom asked on 20 Aug 2010, 03:40 PM

Hi!
   I want auto select first item in radcombobox,like microsoft's combobox,how can i do?
   this is the code and project:

<UserControl x:Class="Silverlight.Rad"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" 
             xmlns:my="clr-namespace:Silverlight" 
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
    <UserControl.Resources>
        <my:Data x:Key="data" />
        <CollectionViewSource x:Key="dataSchoolsViewSource" Source="{Binding Path=Schools, Source={StaticResource data}}" />
        <CollectionViewSource x:Key="dataSchoolsCollegesViewSource" Source="{Binding Path=Colleges, Source={StaticResource dataSchoolsViewSource}}" />
        <CollectionViewSource x:Key="dataSchoolsCollegesDepartmentsViewSource" Source="{Binding Path=Departments, Source={StaticResource dataSchoolsCollegesViewSource}}" />
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>
            <my:MyRadComboBox DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource dataSchoolsViewSource}}" />
            <my:MyRadComboBox DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource dataSchoolsCollegesViewSource}}" />
            <my:MyRadComboBox DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource dataSchoolsCollegesDepartmentsViewSource}}" />
        </StackPanel>
          
    </Grid>
</UserControl>


using Telerik.Windows.Controls;
using System.ComponentModel;
  
namespace Silverlight
{
    public class MyRadComboBox : RadComboBox
    {
        /// <summary>  
        /// Invoked when the <see cref="P:System.Windows.Controls.ItemsControl.Items"/> property changes.   
        /// </summary>  
        /// <param name="e">Information about the change.</param>  
        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
            if (this.ItemsSource != null
                && e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset
                && this.ItemsSource is ICollectionView)
            {
  
                (this.ItemsSource as ICollectionView).CurrentChanged += CollectionView_CurrentChanged;
            }
        }
  
        /// <summary>  
        /// Handles the CurrentChanged event of the CollectionView control.   
        /// </summary>  
        /// <param name="sender">The source of the event.</param>  
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>  
        void CollectionView_CurrentChanged(object sender, EventArgs e)
        {
            var collectionView = this.ItemsSource as ICollectionView;
            if (collectionView != null && this.SelectedItem != collectionView.CurrentItem)
                this.SelectedItem = collectionView.CurrentItem;
        }
  
        /// <summary>  
        /// Called when the selection changes.   
        /// </summary>  
        /// <param name="e">The event data.</param>  
        protected override void OnSelectionChanged(System.Windows.Controls.SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);
            if (this.SelectedItem == null || !(this.ItemsSource is ICollectionView)) return;
            var collectionView = this.ItemsSource as ICollectionView;
            if (collectionView.CurrentItem != this.SelectedItem)
                collectionView.MoveCurrentTo(this.SelectedItem);
        }
    }
}


namespace Silverlight
{
  
    public class Department
    {
        public College College { get; set; }
  
        public string Name
        {
            get;
            set;
        }
    }
  
    public class College
    {
        public School School { get; set; }
  
        ObservableCollection<Department> departments = new ObservableCollection<Department>();
  
        public ObservableCollection<Department> Departments
        {
            get { return departments; }
        }
  
        public string Name
        {
            get;
            set;
        }
    }
  
    public class School
    {
        ObservableCollection<College> colleges = new ObservableCollection<College>();
  
        public ObservableCollection<College> Colleges
        {
            get { return colleges; }
        }
  
        public string Name
        {
            get;
            set;
        }
    }
  
    public class Data
    {
        ObservableCollection<School> schools = new ObservableCollection<School>();
  
        public ObservableCollection<School> Schools
        {
            get { return schools; }
        }
  
        ObservableCollection<Department> departments = new ObservableCollection<Department>();
  
        public ObservableCollection<Department> Departments
        {
            get { return departments; }
        }
  
        public Data()
        {
            School school = new School() { Name = "S1" };
  
            College college = new College() { Name = "S1_Col1", School = school };
            college.Departments.Add(new Department() { Name = "S1_Col1_Dpt1", College = college });
            college.Departments.Add(new Department() { Name = "S1_Col1_Dpt2", College = college });
            college.Departments.Add(new Department() { Name = "S1_Col1_Dpt3", College = college });
            college.Departments.Add(new Department() { Name = "S1_Col1_Dpt4", College = college });
  
            foreach (Department dpt in college.Departments)
            {
                departments.Add(dpt);
            }
  
            school.Colleges.Add(college);
  
            college = new College() { Name = "S1_Col2", School = school };
            college.Departments.Add(new Department() { Name = "S1_Col2_Dpt1", College = college });
            college.Departments.Add(new Department() { Name = "S1_Col2_Dpt2", College = college });
            college.Departments.Add(new Department() { Name = "S1_Col2_Dpt3", College = college });
            college.Departments.Add(new Department() { Name = "S1_Col2_Dpt4", College = college });
            school.Colleges.Add(college);
  
            foreach (Department dpt in college.Departments)
            {
                departments.Add(dpt);
            }
  
            Schools.Add(school);
  
            school = new School() { Name = "S2" };
  
            college = new College() { Name = "S2_Col1", School = school };
            college.Departments.Add(new Department() { Name = "S2_Col1_Dpt1", College = college });
            college.Departments.Add(new Department() { Name = "S2_Col1_Dpt2", College = college });
            college.Departments.Add(new Department() { Name = "S2_Col1_Dpt3", College = college });
            college.Departments.Add(new Department() { Name = "S2_Col1_Dpt4", College = college });
            school.Colleges.Add(college);
            foreach (Department dpt in college.Departments)
            {
                departments.Add(dpt);
            }
  
  
            college = new College() { Name = "S2_Col2", School = school };
            college.Departments.Add(new Department() { Name = "S2_Col2_Dpt1", College = college });
            college.Departments.Add(new Department() { Name = "S2_Col2_Dpt2", College = college });
            college.Departments.Add(new Department() { Name = "S2_Col2_Dpt3", College = college });
            college.Departments.Add(new Department() { Name = "S2_Col2_Dpt4", College = college });
            school.Colleges.Add(college);
            foreach (Department dpt in college.Departments)
            {
                departments.Add(dpt);
            }
  
  
            Schools.Add(school);
        }
    }
}

PROJECT Download

2 Answers, 1 is accepted

Sort by
0
Accepted
Konstantina
Telerik team
answered on 24 Aug 2010, 09:34 AM
Hi lee hom,

Thank you for the code snippets and for the provided project.

To select the first item in RadComboBox you can use the SelectedIndex property or the SelectedItem property of the ComboBox. You can find more information about them in this help article: http://www.telerik.com/help/silverlight/radcombobox-features-selection.html

Hope this helps. If you need further assistance please let us know.

Sincerely yours,
Konstantina
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
lee hom
Top achievements
Rank 1
answered on 26 Aug 2010, 10:07 AM
OH  I SEE, override the itemschaned funtion.
thank you
Tags
ComboBox
Asked by
lee hom
Top achievements
Rank 1
Answers by
Konstantina
Telerik team
lee hom
Top achievements
Rank 1
Share this question
or