?
Solved

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

Posted on 2010-01-11
10
Medium Priority
?
870 Views
Last Modified: 2013-11-12
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
Comment
Question by:CMCONE
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 26288729
?
postit t = (postit)this.FindName("postIT_3");
0
 
LVL 1

Author Comment

by:CMCONE
ID: 26290152
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 26292027
grid.Children.Add(p);

Does grid is the name on your grid?

try to make another name

ie.

<Grid x:Name="MyGrid" ...
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 1

Author Comment

by:CMCONE
ID: 26293997
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 26294182
have you tried

postit p = this.parent.findname(...);
0
 
LVL 1

Author Comment

by:CMCONE
ID: 26294729
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 26294754
no try this


(this.Parent as Grid).FindName("postIT_1"),
0
 
LVL 1

Author Comment

by:CMCONE
ID: 26294855
I get a "cannot implicitly convert type 'object' to 'Taskboard.postit'" error.
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 2000 total points
ID: 26294875
(postit)(this.Parent as Grid).FindName("postIT_1");


to see wether you can see it
0
 
LVL 1

Author Closing Comment

by:CMCONE
ID: 31675754
Thank you for all your help.  This works perfectly!  It finds the other controls now!  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question