Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Access User Control (Textbox) Used Multiple Times
9 replies. Latest Post by ccoombs on October 29, 2008.
(0)
surrounded
Member
15 points
47 Posts
10-29-2008 10:35 AM |
I have a textbox user control. I add this control to a stack panel twice; need to get two different addresses. When accessing the textbox value, how can I differentiate between the two textboxes. They both have the same x:Name, and setting a Tag does not seem to work. The tag is always null.
If I loop through Layout.Children it only has one UIElement because the TextBox is all that's defined on Textbox.xaml.
Thanks....
ccoombs
Contributor
5168 points
758 Posts
10-29-2008 10:43 AM |
you need to create a new instance of the control...
david.ya...
397 points
69 Posts
10-29-2008 10:48 AM |
Hi
you can modify the properties of user controls using Expression Blend , click on each textbox and change their name.
Regards
10-29-2008 10:49 AM |
Do you mean the following:
DP.
I am doing this already. The logic for the textboxes is handled in Textbox.xaml.cs, but of course it is the same logic for both of them.
OR
do you mean I need to create a new user control for the second texbox.
10-29-2008 10:51 AM |
David, there is only one textbox in the user control, so I cannot change the names in Expression Blend as there is only one name
10-29-2008 11:00 AM |
My fault; I thought you meant you were trying to reuse the same instance.
as i understand it, you've got a usercontrol which contains a textbox. your usercontrol is added to a stack panel, and you need to be able to address the textbox contained in each usercontrol respectively.
If you have two instances of your usercontrol (pFirstTextbox and pTextbox), then you have to just reference the textbox contained in those instances. they'll both have the same name as defined in xaml (like you said), but they're tied to a specific instance. So if your textbox's x:Name is "baseTextBox", then you just do:
pFirstTextbox.baseTextBox
pTextbox.baseTextBox
10-29-2008 1:24 PM |
Thanks, but I'm still unclear how to access pFirstTextbox and pTextbox (which are defined in Page.xaml.cs) in Textbox.xaml.cs. Textbox.xaml.cs contains the logic to interact with the textbox, such as MouseEnter, KeyUp, etc.
10-29-2008 1:38 PM |
you never said you wanted to manipulate one control from the other. i'm still not even quite sure that's what you're trying to accomplish. can you explain what you're trying to do?
if you're just trying to get one instance to react to the actions occuring in the other, i would suggest creating your own events for the user control, and wiring them up wherever your instances are defined (Page.xaml.cs).
10-29-2008 1:46 PM |
In Page.xaml.cs, I add one or more textboxes to a stack panel depending on a user selection. Once the textboxes are added, the user then enters information in the textbox. I had created a separate user control for a generic textbox (Textbox.xaml) to handle all this. It's a TextBox, nothing special, just has all the event handling in Textbox.xaml.cs instead of Page.xaml.cs
So for example, the user may want to map a route between two street addresses. The program then adds two textbox user controls to the stackpanel. He/she would enter the first street address in one textbox, the second address in the next textbox. The problem, as you can see, is how to differentiate between the two textboxes in the event handling (Textbox.xaml.cs) - as they are created with the same user control. Thought there would be a way to do this, such as using the Tag property or something, but haven't figured it out.
10-29-2008 1:59 PM |
your user controls are instanced. the events only apply to 'this' instance.
instance 1 has no knowledge of instance 2, or any other instance for that matter. imagine if you had 1000 instances of your control. if you've got a keydown event wired up to your textbox, it will only fire for that single instance of your usercontrol.