Hi,
I have a RadTreeView with checkboxes(Implemented using MVVM as suggested by Telerik). To work with Keyboard events(checkbox should be checked when we enter a space), We implemented KeyDown event and is working fine.
Now if I select an item and enter space it gets checked, if I click on tab the next item gets focus but still the first item is in selected mode. Is there any way to get unselected, when we tab out . Please provide your inputs.
Thanks,
Subhashini
5 Answers, 1 is accepted
RadTreeViewItem is UIElement and therefore it has LostFocus routed event. For routed events you can subscribe witht he following syntax:
this
.AddHandler(RadTreeViewItem.LostFocusEvent,
new
RoutedEventHandler(item_LostFocus),
true
);
In the handler you can decide whether to change the container properties directly or the ViewModel's properties:
void
item_LostFocus(
object
sender, RoutedEventArgs e)
{
var container = e.OriginalSource
as
RadTreeViewItem;
if
(container !=
null
)
{
var model = container.DataContext;
//
// code for setting model properties here.
//
// code setting properties of container below=>
container.IsSelected =
false
;
}
}
We hope this fits well in your scenario.
Regards,
Petar Mladenov
Telerik
Similarly to LostFocus, you can use GotFocus event and perform the selection logic in its handler. Hope this helps.
Regards,
Petar Mladenov
Telerik
Hi,
I have a treeview with checkboxes(Implemented using trivial state MVVM as suggested by Telerik). Now if I select radtreeview items with shift+down arrow(say five items are selected). When I click "space" key, only last selected item is checked. This is because the radtreeview.SelectedItems is giving only the last selected item. Is it a known issue?
If I do the same workflow with Shift+Space key, then all the selected items are checked. Can you provide inputs how can we acheive this functionality only with space key for all the selected items to get checked.
Thanks,
Subhashini
In Extended SelectionMode, the SelectedItems collection will give you the collection of all selected Viewmodels in databound scenario. If you mean CheckedItems, please note that there are some known bugs with this collection and we highly encourage you to avoid using it.
In the attached solution you can find a possible approach for checking all selected items in code behind.
In PreviewKeyDown event you can iterate the selected models and set their property:
private void tree_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
foreach (var item in this.tree.SelectedItems)
{
(item as DataItem).IsChecked = true;
}
e.Handled = true;
}
}
The built-in logic for checking items with space will work only for not-databound trees and built-in checkboxes. Also it will only check the item which holds the keyboard focus.
Regards,
Petar Mladenov
Telerik