[英]TreeViewitem can't select = null
As per your latest image, e.OriginalSource
is a TreeView
. 根据您的最新图像,
e.OriginalSource
是TreeView
。 When you cast e.OriginalSource
like this: 当您像这样投射
e.OriginalSource
:
TreeViewItem item = e.OriginalSource as TreeViewItem;
... item
will always be null
. ...
item
将始终为null
。 This is because your type cast cannot convert e.OriginalSource
into your desired type. 这是因为您的类型转换无法将
e.OriginalSource
转换为所需的类型。 If you this hard cast instead: 如果您改用这种强制转换:
TreeViewItem item = (TreeViewItem)e.OriginalSource;
You would get a CastException
that explains you why. 您将获得一个
CastException
来解释原因。
So what you got to do is to cast into the correct type like this: 因此,您要做的是像这样转换为正确的类型:
TreeViewItem tree = e.OriginalSource as TreeView; // I am a tree view :-)
... and then select from tree.Items
the item you require. ...,然后从
tree.Items
选择tree.Items
的项目。
e.Source
and e.OriginalSource
refer to the TreeView
not the TreeViewItem
selected. e.Source
和e.OriginalSource
引用的是TreeView
而不是选择的TreeViewItem
。
Try this to get the selected item: 尝试此操作以获取所选项目:
TreeViewItem item = e.NewValue as TreeViewItem;
Or this to get the previously selected item: 或这样获得先前选择的项目:
TreeViewItem item = e.OldValue as TreeViewItem;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.