I have RadTreeView with MultiSelect = true; When I’m programmatically select any node in tree like
radTreeView.Nodes[0].Nodes[0] = true;
radTreeView.Nodes[0].Nodes[1] = true;
I expect then event SelectedNodeChanged will be fired, but it isn’t. It is a big problem, because I can’t handle when my nodes being selected.
We are using Telerik RadControls_WinForms_2012_3_1211
p.s. when I select node by this way: radTreeView.SelectedNode = radTreeView.Nodes[0].Nodes[1], event fires successfully, but using this method I can’t select multiple nodes from code.
How do you suggest to resolve this?
4 Answers, 1 is accepted
Thank you for the writing.
Currently the SelectedNodeChanged event is firing only for current node change which is bug. We already have a related PITS item: http://www.telerik.com/support/pits.aspx#/details/Issue=12205 . Feel free to add your vote for it.
Currently, you can use the following workaround:
1. Create custom tree node that inherits the RadTreeNode
2. Via reflection call the OmSelectedNodeChanged method of the tree view when "Selected" property of the node is changed. This will fire the SelectedNodeChanged event of the tree view.
public
class
MyRadTreeNode : RadTreeNode
{
public
MyRadTreeNode(
string
text)
:
base
(text)
{
}
private
readonly
static
MethodInfo OnSelectedNodeChangedMethodInfo =
typeof
(RadTreeViewElement).GetMethod(
"OnSelectedNodeChanged"
, BindingFlags.NonPublic | BindingFlags.Instance,
null
,
new
Type[] {
typeof
(RadTreeNode) },
null
);
protected
override
void
OnNotifyPropertyChanged(PropertyChangedEventArgs args)
{
base
.OnNotifyPropertyChanged(args);
if
(args.PropertyName ==
"Selected"
&&
this
.TreeViewElement !=
null
)
{
OnSelectedNodeChangedMethodInfo.Invoke(
this
.TreeViewElement,
new
object
[] {
this
});
}
}
}
3. Use your custom nodes instead the default ones. If your RadTreeView is using data source and the nodes are creating automatically you can subscribe to CreateNode event of the grid and replace them with the custom ones:
void
radTreeView1_CreateNode(
object
sender, CreateTreeNodeEventArgs e)
{
e.Node =
new
MyRadTreeNode(e.Node.Text);
}
If you are creating your nodes manually you can use the following example:
RadTreeNode Node1 =
new
RadTreeNode(
"Node1"
);
RadTreeNode Node2 =
new
RadTreeNode(
"Node2"
);
RadTreeNode Node3 =
new
MyRadTreeNode(
"Node3"
);
RadTreeNode Node4 =
new
MyRadTreeNode(
"Node4"
);
radTreeView1.Nodes.Add(Node1);
radTreeView1.Nodes.Add(Node2);
Node1.Nodes.Add(Node3);
Node1.Nodes.Add(Node4);
Attached is a sample project that comprises the code above.
I hope this information helps and please excuse us for the introduced inconvenience.
Kind regards,
Anton
the Telerik team
I ran into this same problem and was using the code provided by Telerik for a while. However, I just upgrade to the latest version (Q1 2015 SP1) and it appears the signature of OnSelectedNodeChanged has changed so the reflection logic started breaking. Instead, I've started using the following code and it works:
private
static
readonly
MethodInfo OnSelectedNodeChangedMethodInfo =
typeof
(RadTreeViewElement).GetMethod(
"OnSelectedNodeChanged"
,
BindingFlags.NonPublic | BindingFlags.Instance,
null
,
new
Type[] {
typeof
(RadTreeNode),
typeof
(RadTreeViewAction) },
null
);
protected
override
void
OnNotifyPropertyChanged(PropertyChangedEventArgs args)
{
base
.OnNotifyPropertyChanged(args);
if
(args.PropertyName ==
"Selected"
&& TreeViewElement !=
null
)
{
OnSelectedNodeChangedMethodInfo.Invoke(TreeViewElement,
new
object
[] {
this
, RadTreeViewAction.ByMouse });
}
}
I randomly chose RadTreeViewAction.Mouse. I'm not sure if another value would be more appropriate but this seemed to work.
In my case, I'm actually not doing any selection via code. My use case is as follows:
- Have a tree view with Multi-Select = true
- Select a Node by clicking on it - Event fires as expected
- Unselect that same node by holding Ctrl key and clicking the node. The node becomes unselected but the event doesn't fire
With the workaround above, the event does fire.
Thank you for writing.
I would like to note that the problem with selection when clearing the nodes was fixed in Q3 2013 SP2. Here is the feedback item for your reference.
As to the case with multiple selection I confirm that it is an issue with RadTreeView. I have logged it in our feedback portal. You can track its progress, subscribe for status changes and add your vote/comment to it on the following link - feedback item.
I have also updated your Telerik points.
Should you have further questions, I would be glad to help.
Dess
Telerik