6 Answers, 1 is accepted
Depending on your scenario I can suggest you couple approaches for achieving this:
- If you are populating the RadTreeView control with RadTreeViewItems statically in XAML you can use the tree's PreviewEdited event. Here is an example:
private
void
RadTreeView_PreviewEdited(
object
sender, RadTreeViewItemEditedEventArgs e)
{
var treeViewItem = e.OriginalSource
as
RadTreeViewItem;
if
(another node
in
the tree contains the same name
as
the name of the edited item)
// you can use the e.NewValue to get the edited name and see if it matches with another name in the tree
{
e.Handled =
true
;
treeViewItem.IsInEditMode =
false
;
treeViewItem.Focus();
}
}
- If you use the treeview in a data binding scenario you can use the EventToCommandBehavior and ICommands in your view model to check for a valid value. For your convenience I prepared a sample project demonstrating this approach. Please take a look at it and let me know if it helps.
Regards,
Martin
Telerik
Hello Martin,
My TreeView contains "Session","Session (1)","session (2)"...etc...
For example I am going to edit "Session (1)" item as "Session" it should take oldame("Session (1)".why because alrady I have "Session".As per your code "var treeViewItem = e.OriginalSource as RadTreeViewItem;"
e.OriginalSource gives Edited Name("Session") it should return "Session (1)".
if I call like "var treeViewItem = e.OldValue as RadTreeViewItem;" ,treeViewItem returns null.
and also please find the attaches files also. Please help me how to proceed this.
The RadTreeViewItem's header (name) is already changed in the Edited event, so you cannot use it to get the old value. About the e.OldValue - it returns null because the property contains string, but not RadTreeViewItem object. Furthermore, the property contains the header of the item before the edit and you can use it for your implementation.
private
tv_PreviewEdited(
object
sender, RadTreeViewItemEditedEventArgs e)
{
if
(tv.SelectedItem.ToString() ==
"Session"
)
{
var oldName = e.OldValue.ToString();
....
}
}
private
tv_PreviewEdited(
object
sender, RadTreeViewItemEditedEventArgs e)
{
if
(tv.SelectedItem.ToString() ==
"Session"
)
{
e.Handled =
true
;
}
}
Regards,
Martin
Telerik
private tv_PreviewEdited(object sender, RadTreeViewItemEditedEventArgs e){ if (tv.SelectedItem.ToString() == "Session")
{
string _str = "Plese select different name!";
Utils.ShowErrorMessage(_str, true, "Warning!!!!!");
e.Handled = true;
....}
popup window opens and if I select ok, and The selected node(RadTreeViewItem) should be editable mode.(Just It was selected mode only)
Now I am trying that, but its not happening.
Please help me.
I tested the reported scenario and the treeview item does not exit the edit mode when a popup window opens. Can you please take a look at the attached project and let me know if I am missing something?
Regards,
Martin
Telerik