azamsharp
azamsharp

Reputation: 20066

The Background Property Problem in WPF

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

Answers (2)

Kenan E. K.
Kenan E. K.

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

Andrew Hare
Andrew Hare

Reputation: 351516

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

Upvotes: 1

Related Questions