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