Reputation: 540
I have a couple of ellipses which I need to select by its name, is there any way to do so?
My XAML:
<Grid Name="Fields"
Height="300"
Width="340">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Ellipse x:Name="f00" Margin="10" Fill="White" Grid.Row="0" Grid.Column="0" />
<Ellipse x:Name="f01" Margin="10" Fill="White" Grid.Row="0" Grid.Column="1" />
<Ellipse x:Name="f02" Margin="10" Fill="White" Grid.Row="0" Grid.Column="2" />
<Ellipse x:Name="f03" Margin="10" Fill="White" Grid.Row="0" Grid.Column="3" />
<Ellipse x:Name="f04" Margin="10" Fill="White" Grid.Row="0" Grid.Column="4" />
<Ellipse x:Name="f05" Margin="10" Fill="White" Grid.Row="0" Grid.Column="5" />
<Ellipse x:Name="f06" Margin="10" Fill="White" Grid.Row="0" Grid.Column="6" />
</Grid>
If possible, I would prefer using linq
var a = from System.Windows.Shapes.Ellipse ellipse in this.Fields
where ellipse.Name == "f03"
select ellipse;
Upvotes: 0
Views: 394
Reputation: 6948
Using the FindName method and casting it as Ellipse should work:
var thisellipse = (Ellipse)this.FindName("f03");
Upvotes: 1
Reputation: 883
Here is one solution.
foreach (UIElement item in Fields.Children)
{
Ellipse el = (Ellipse)item;
if (el.Name == "f03")
{
// Do something
}
}
Upvotes: 1