Reputation: 9
OK so I have to work with lots of toggle button and textbox, so I named them in such way that adding letter "Q" to button's name will give name of the textbox that the button corresponds to. I have link all the button to the same method on checked event, and using this naming mechanism I, hope to manipulate the textbox corresponding the button.
What I have done till now is, generate an name of the textbox in string format. But i don't know to how to use this string to manipulate the textbox.
private void OnbuttonClick(object sender, RoutedEventArgs e)
{
ToggleButton tb = (ToggleButton)sender;
if(tb.IsChecked==true)
{
string tbnam = ((ToggleButton)sender).Name;
string name2 = tbnam + "Q";
?????? (what happens from this point onward)
}
}
"name2" is the name of textbox that corresponds to the toggle button of name "tbnam".
I hope the I have made the problem clear.
Upvotes: 0
Views: 31
Reputation: 37066
In WPF, a much better way to do this would to expose a collection of objects in the viewmodel, and display in the UI with an ItemsControl
with an ItemTemplate
that would dynamically create as many iterations of ToggleButton
/TextBox
pairs as needed. I'd be happy to go into how to do that, if you're interested.
But for now, you can get your code working using the FindName(string)
method:
private void Button_Click(object sender, RoutedEventArgs e)
{
ToggleButton tb = (ToggleButton)sender;
if (tb.IsChecked == true)
{
string name2 = tb.Name + "Q";
TextBox tbox = FindName(name2) as TextBox;
// Check to see if tbox is null, do stuff with it
}
}
Upvotes: 1