tags:

views:

70

answers:

2

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));
+1  A: 

The compiler is unable to infer the type of element from the expression you have provided.

Andrew Hare
+6  A: 

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
kek444