So I've just discovered the null coalescing ternary operator but I can't seem to figure out why my variable is still being set to null in the line of code below.
MenuItem parent = (treeView.SelectedItem ?? treeView.Items) as MenuItem;
treeView.SelectedItem may or may not be null, but it's never a
treeView.Items may or may not be null, but it's never a
(treeView.SelectedItem ?? treeView.Items)
...returns the value of
treeView.SelectedItem if that value isn't null. If that value is null, the expression returns
treeView.Items. Probably better to say "it returns the first non-null operand".
Whichever of those you get, you then use
as to try to cast it to a
MenuItem. The cast never succeeds, because the actual object you're casting can never be cast to
null when the cast is not possible.
So you always get
When you get a situation like this, put in a breakpoint and hover the mouse over all the operands for the expression. Once you know what the operands really are, you can select subexpressions like
(treeView.SelectedItem ?? treeView.Items) and hover over those. If you have any questions, don't just try to stare down the code and sweat it out from first principles like Bertrand Russell. Looking at the values in the debugger will provide critical information you'd never think of.
But your real problem is that you have, in some way I haven't identified, populated a WPF TreeView with MenuItems, and you want to get the selected menu item back out. I've tried to reproduce what you did, and the Items collection on my treeview is full of MenuItems, but you say you're seeing TreeViewItems. So I'm not sure what's going on.
This strikes me as a bad way to populate a TreeView in any case. I'd suggest a collection of a mini-viewmodel class, bound to the TreeView's ItemsSource property. There are plenty of examples of this on Stack Overflow. That's the way WPF wants you to do it, and I've found that WPF is extremely unfriendly if you don't do things the way it wants you to.