Hi
I have a treeview binded to hierarchical objects with four levels: TrainingProgram/Period/Subject/Teachingunit.
The user can expand/collapse the tree.
I want to refresh TreeView after reloading the data (because some operations are done directly on the server and then a reload is required).
Everything works perfect except the selection of the last select item before reloading:
CourseRadTreeView.SelectedItem = lastSelectedItem;
I found the article with the idea of the IsSelected property, but this is also not working,
because after reloading the selection in the property was loosing:
http://www.telerik.com/support/kb/silverlight/treeview/radtreeview-and-hierarchicaldatatemplate.aspx
What to do?
Thanks
Björn.
XAML:
XAML.cs:
I have a treeview binded to hierarchical objects with four levels: TrainingProgram/Period/Subject/Teachingunit.
The user can expand/collapse the tree.
I want to refresh TreeView after reloading the data (because some operations are done directly on the server and then a reload is required).
Everything works perfect except the selection of the last select item before reloading:
CourseRadTreeView.SelectedItem = lastSelectedItem;
I found the article with the idea of the IsSelected property, but this is also not working,
because after reloading the selection in the property was loosing:
http://www.telerik.com/support/kb/silverlight/treeview/radtreeview-and-hierarchicaldatatemplate.aspx
What to do?
Thanks
Björn.
XAML:
<
telerik:HierarchicalDataTemplate
x:Key
=
"Teachingunit"
>
<
Grid
>
<
StackPanel
Orientation
=
"Horizontal"
Grid.Column
=
"0"
HorizontalAlignment
=
"Left"
>
<
TextBlock
Text
=
"{Binding StartDatetime, StringFormat='ddd dd.MM.yyyy, HH:mm'}"
/>
</
StackPanel
>
<!-- Functions ... -->
</
Grid
>
</
telerik:HierarchicalDataTemplate
>
<
telerik:HierarchicalDataTemplate
x:Key
=
"Subject"
ItemTemplate
=
"{StaticResource Teachingunit}"
ItemsSource
=
"{Binding Path=TeachingunitSortedCollection.View}"
>
<
Grid
>
<
TextBlock
Text
=
"{Binding Designation}"
HorizontalAlignment
=
"Left"
/>
<!-- Functions ... -->
</
Grid
>
</
telerik:HierarchicalDataTemplate
>
<
telerik:HierarchicalDataTemplate
x:Key
=
"Period"
ItemTemplate
=
"{StaticResource Subject}"
ItemsSource
=
"{Binding Path=SubjectSortedCollection.View}"
>
<
Grid
>
<
TextBlock
Text
=
"{Binding Designation}"
HorizontalAlignment
=
"Left"
/>
<!-- Functions ... -->
</
Grid
>
</
telerik:HierarchicalDataTemplate
>
<
telerik:HierarchicalDataTemplate
x:Key
=
"TrainingProgram"
ItemTemplate
=
"{StaticResource Period}"
ItemsSource
=
"{Binding Path=PeriodSortedCollection.View}"
>
<
Grid
>
<
TextBlock
Text
=
"{Binding Designation}"
HorizontalAlignment
=
"Left"
/>
<!-- Functions ... -->
</
Grid
>
</
telerik:HierarchicalDataTemplate
>
<
telerikNav:RadTreeView
x:Name
=
"CourseRadTreeView"
IsLineEnabled
=
"True"
SelectionMode
=
"Single"
IsEditable
=
"False"
ItemsSource
=
"{Binding Path=CourseDataSource}"
ItemTemplate
=
"{StaticResource TrainingProgram}"
Loaded
=
"CourseRadTreeView_Loaded"
Selected
=
"CourseRadTreeView_Selected"
Expanded
=
"CourseRadTreeView_ExpandedCollapse"
Collapsed
=
"CourseRadTreeView_ExpandedCollapse"
>
</
telerikNav:RadTreeView
>
XAML.cs:
public
partial
class
CourseTreeView : UserControl
{
public
CourseTreeView()
{
InitializeComponent();
// when the data are reloaded update the treeview
AppMessages.ReloadedOperationMessage.Register(
this
, OnReloadedOperation);
}
private
void
OnReloadedOperation(
bool
loaded)
{
ReexpandNodes();
ReselectedLastSelectedItem();
}
// ===================================================================
#region Selected Item
private
ICourse _lastSelectedItem =
null
;
private
void
CourseRadTreeView_Selected(
object
sender, RadRoutedEventArgs e)
{
var radTreeViewItem = e.Source
as
RadTreeViewItem;
if
(radTreeViewItem !=
null
)
{
// expand the new selected item
radTreeViewItem.IsExpanded =
true
;
// remember the last selection
_lastSelectedItem = radTreeViewItem.Item
as
ICourse;
CourseDetailViewModel.OnSelectionChangingCommand(radTreeViewItem.Item
as
ICourse);
}
}
private
void
ReselectedLastSelectedItem()
{
// TODO: bbee / 20110825 - is not highlighting the selected item!
//if (_lastSelectedItem != null)
//{
// CourseRadTreeView.SelectedItem = _lastSelectedItem;
//}
}
#endregion
// ===================================================================
#region Expand/Collapse
private
readonly
Dictionary<
string
,
string
> _expandedPaths =
new
Dictionary<
string
,
string
>();
private
void
CourseRadTreeView_ExpandedCollapsed(
object
sender, RadRoutedEventArgs e)
{
var radTreeViewItem = e.Source
as
RadTreeViewItem;
if
(radTreeViewItem !=
null
)
{
// returns the full path with the delimiter '\': used ToString() from the course items
var path = radTreeViewItem.FullPath;
if
(
string
.IsNullOrEmpty(path))
return
;
if
(radTreeViewItem.IsExpanded)
{
if
(!_expandedPaths.ContainsKey(path))
{
_expandedPaths.Add(path,
""
);
}
}
else
{
if
(_expandedPaths.ContainsKey(path))
{
_expandedPaths.Remove(path);
}
}
}
}
private
void
ReexpandNodes()
{
// don't listen to the expand/collapse event during expanding
CourseRadTreeView.Expanded -= CourseRadTreeView_ExpandedCollapsed;
CourseRadTreeView.Collapsed -= CourseRadTreeView_ExpandedCollapsed;
foreach
(var expandedPath
in
_expandedPaths)
{
var path = expandedPath.Key;
CourseRadTreeView.ExpandItemByPath(path);
}
// listen again
CourseRadTreeView.Expanded += CourseRadTreeView_ExpandedCollapsed;
CourseRadTreeView.Collapsed += CourseRadTreeView_ExpandedCollapsed;
}
#endregion