Frage

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;
War es hilfreich?

Lösung

Using the FindName method and casting it as Ellipse should work:

var thisellipse = (Ellipse)this.FindName("f03");

Andere Tipps

Here is one solution.

foreach (UIElement item in Fields.Children)
{
    Ellipse el = (Ellipse)item;
    if (el.Name == "f03")
    {
        // Do something
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top