Reputation: 12025
I'm searching for a linq-to-objects way to get a TreeViewItem from a treeView.
I want to do something like this:
var node =
from TreeViewItem childs in tree.Items
where ((int) childs.Tag) == 1000
select childs;
string tag = ((TreeViewItem)node).Tag.ToString();
Then I want to append children to this node.
Thanks.
Upvotes: 1
Views: 4704
Reputation: 532475
You'll want to use FirstOrDefault to extract the first matching element from the enumeration created by the query. After checking that it's not null, you can then operate on it like you normally would.
var query =
from TreeViewItem childs in tree.Items
where ((int) childs.Tag) == 1000
select childs;
var node = query.FirstOrDefault();
if (node != null)
{
...
}
Note that you won't need the cast any longer since FirstOrDefault will return a TreeViewItem.
Upvotes: 2