1

I am new to WindowsApp Development and I was trying to access XAML elements through C# code using an array. For example I have few ellipses in my XAML code-

<Ellipse Fill="#FFF4F4F5" x:Name="E_0" Grid.Row="0" Grid.Column="2" Stroke="Black" RenderTransformOrigin="0.474,5.849"  Visibility="Visible" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Ellipse Fill="#FFF4F4F5" x:Name="E_1" Grid.Row="0" Grid.Column="3" Stroke="Black" RenderTransformOrigin="0.474,5.849"  Visibility="Visible" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<Ellipse Fill="#FFF4F4F5" x:Name="E_2" Grid.Row="0" Grid.Column="4" Stroke="Black" RenderTransformOrigin="0.474,5.849"  Visibility="Visible" HorizontalAlignment="Center" VerticalAlignment="Center"/>

Now I want to work with them in C# in a loop so I am doing something similar to the following-

string s1="E_";
double width=500;
for (int i = 0; i < 2;i++ )
 {
    string name_i = s1 + i.ToString();
    name_i.Width = width / 2;
 }

But name_i.width gives me an error. So, do I have to use the actual names, is there no way I can use an array or string? Using the actual names will defeat the purpose as I have about 50 such elements which I need to work upon.

8
  • And if you'd group the Ellipse objects in a container and give that container a variable name, couldn't you loop through the Children of that container and affect every eclipse thereby? Commented Feb 3, 2016 at 19:49
  • Maybe, I am not sure, actually I do have all the Ellipses in a Grid by the name of Game. But if I enumerate the children, how can I check only for the Ellipse elements and not other elements present in that Grid? Commented Feb 3, 2016 at 19:55
  • 1
    Will that not require two loops- an outer loop iterating over Children and the inner loop iterating over my var ellipses. Is there some way, I can do this using a single array instead of Container because if it is possible, the n I think it would be more efficient. Commented Feb 3, 2016 at 20:08
  • 1
    No, I meant you either group them in a container and iterate over the Children, or you group them in an array by code with the Ellipse[] ellipses = { ... }; and iterate over this array, but not both at once. Commented Feb 3, 2016 at 20:10
  • 1
    Since you've defined ` x:Name="E_0"` etc. in every eclipse you should have this variable name available, so that works and should compile. Eh, maybe with a new Ellipse[] before the { but I think that has become redundant with newer C# versions. Commented Feb 3, 2016 at 20:16

2 Answers 2

2

As Maximillian proposed, you can use the parent container and iterate over its children:

Xaml:

 <StackPanel Name="StackPanelContainer">
      <Grid></Grid>
      <Ellipse Name="E_0"></Ellipse>
      <Ellipse Name="E_1"></Ellipse>
      <Ellipse Name="E_2"></Ellipse>
    </StackPanel>

Codebehind:

        //if you are sure every child element is a ellipse, you can use:
        foreach (Ellipse child in StackPanelContainer.Children)
        {
            child.Width = 100;
        }

        //if there are also other elements, and also check if name starts with "E_"
        foreach (object child in StackPanelContainer.Children)
        {
            var ellipse = child as Ellipse;
            if (ellipse != null && ellipse.Name.StartsWith("E_"))
            {
                ellipse.Width = 100;
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but is there some other way to achieve this using arrays. This is because this will mostly not work if I want to work with just some of the Ellipses. Say I want to change Width of only some 15 out of 50 Ellipses, then comparing each of them with their name will involve two loops(if I am correct)-one outer loop iterating over the Children and the other loop which will iterate over my array where I will compare the names of the Children.
2

Use FindName() method.

string s1 = "E_";
double width = 500;
for (int i = 0; i < 2; i++)
{
    string name_i = s1 + i.ToString();

    var ellipse = FindName(name_i) as Ellipse;

    if (ellipse != null)
    {
        ellipse.Width = width / 2;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.