Corio
Corio

Reputation: 395

How can I get to know a type of an object in Canvas.Children

How can I get to know a type of an object in Canvas.Children in WPF? For example I have an Ellipse and Rectangle shown on a Canvas. How to get a type of Canvas.Children[0]? I have something like this but it says, that "The given expression is never of the provided ('System.Windows.Shapes.Ellipse') type". I need to check it: if (canvas.Children[0].GetType() is System.Windows.Shapes.Ellipse)

Upvotes: 2

Views: 2440

Answers (2)

Nitin Purohit
Nitin Purohit

Reputation: 18580

If you just want to know if you element is ellipse or rectangle you can directly say

if(canvas.Children[0] is Ellipse)

or

if(canvas.Children[0] is Rectangle)

Upvotes: 0

nemesv
nemesv

Reputation: 139758

You cannot use is here because GetType() returns a Type then you need to use typeof (MSDN):

if (canvas.Children[0].GetType() == typeof(System.Windows.Shapes.Ellipse))

Or you can just use directly is on canvas.Children[0]

if (canvas.Children[0] is System.Windows.Shapes.Ellipse)

Upvotes: 5

Related Questions