Gratzy
Gratzy

Reputation: 2898

UIElement does not contain a definition for 'Name'

This is the code I'm working with:

foreach (UIElement child in cnvsLinkScreen.Children)
{
   if (child.Name.Contains("LinkObject");
       cnvsLinkScreen.Children.Remove(child);
}

I have the different objects named like "LinkObject1" and "Line1" (plus other types). I could run several loops and remove by name but I thought this would be easier to do.

When I run the code:

        foreach (UIElement child in cnvsLinkScreen.Children)
        {
            Debug.WriteLine(child);
        }

and break on the Debug; I see the name of the child object listed under the "Name" property in the child object.

However, I can't reference it in the first code example. I get the compile error:

Error   CS1061  'UIElement' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'UIElement' could be found (are you missing a using directive or an assembly reference?)  

How do I reference the "Name" property of the UIElement?

Upvotes: 0

Views: 629

Answers (1)

mm8
mm8

Reputation: 169330

Cast to FrameworkElement:

foreach (FrameworkElement child in cnvsLinkScreen.Children.OfType<FrameworkElement>())

That's where the Name property is defined. A UIElement has no Name property.

Upvotes: 2

Related Questions