Reputation: 20066
WPF Controls are divided into various baskets. Some controls belong to System.Windows.Controls namespace and other belong to Panel and other stuff. I am interested in getting the control as a Panel or Control type so I can change the Background property. The following code is not working:
var element = ((sender as Panel) ?? (sender as Control));
Upvotes: 0
Views: 246
Reputation: 14111
Unfortunately, the "magical" var keyword is still statically (at compile time) resolved, what you might be thinking of is the new dynamic
C# 4.0 keyword.
Otherwise, there's no other way to do it other than
Panel panelElement = sender as Panel;
Control controlElement = sender as Control;
if(panelElement != null)
//do stuff for panel
else if(controlElement != null)
//do stuff for control
Upvotes: 6
Reputation: 351516
The compiler is unable to infer the type of element
from the expression you have provided.
Upvotes: 1