Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 871
  • Last Modified:

How do I reference dynamically created controls in Silverlight/XAML/C#?

I have dynamically created controls in a Silverlight program via  a loop:
//inside loop...
postit p = new postit();
p.Name = "postIT_" + Convert.ToString(a);
grid.Children.Add(p);
a++
I am able to move this control (post-it note) up and down the grid with a button click on the control (using postit p = this;).  However, I need to reference the other dynamically created controls (post it notes) to move them up or down the grid depending on where the clicked control goes.  (i.e. if "postIT_2" moves down, then "postIT_3" will move up).  Any ideas?
0
CMCONE
Asked:
CMCONE
  • 5
  • 5
1 Solution
 
EDDYKTCommented:
?
postit t = (postit)this.FindName("postIT_3");
0
 
CMCONEAuthor Commented:
That makes sense, but unfortunately doesn't work.  For some reason, postit t = null when I run it.  

I've found some things about using RegisterName, but I don't know if I have the correct assembly because I cannot find this anywhere in intellisense.
0
 
EDDYKTCommented:
grid.Children.Add(p);

Does grid is the name on your grid?

try to make another name

ie.

<Grid x:Name="MyGrid" ...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CMCONEAuthor Commented:
My grid is actually named "grid1", but it is in a different class.  I've attached a code snippet.
//target is "grid1"  (This is in the MainPage.xaml.cs
private void create_dataholders(Grid target)
        {
            int i = 5;
            int a = 0;

            while (a<i)
            {
                postit p = new postit();
                p.Name = "postIT_" + Convert.ToString(a);
                p.textBlock1.Text = p.Name;
                target.Children.Add(p);
                a++;
       
            }
//This is in the postit.xaml.cs, so it is a different class.  It works fine, but I can't figure out how to access the other postit...
 private void buttonUP_Click(object sender, RoutedEventArgs e)
        {
            postit p = this;
            int rp = Convert.ToInt16(p.GetValue(Grid.RowProperty));
            p.SetValue(Grid.RowProperty, (rp - 1));
        }

Open in new window

0
 
EDDYKTCommented:
have you tried

postit p = this.parent.findname(...);
0
 
CMCONEAuthor Commented:
The FindName method is not allowed under the Parent.

I tried postit p2 = (Grid)this.Parent.FindName("postIT_1"), but it won't allow the FindName.

Thanks for your help so far.
0
 
EDDYKTCommented:
no try this


(this.Parent as Grid).FindName("postIT_1"),
0
 
CMCONEAuthor Commented:
I get a "cannot implicitly convert type 'object' to 'Taskboard.postit'" error.
0
 
EDDYKTCommented:
(postit)(this.Parent as Grid).FindName("postIT_1");


to see wether you can see it
0
 
CMCONEAuthor Commented:
Thank you for all your help.  This works perfectly!  It finds the other controls now!  
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now