0

I am working on a C# WPF project. I have multiple buttons that I want to essentially do the same thing, but post the results back into different textboxes. Similar to the xaml snippet below:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0">
        <TextBox x:Name="ATextbox"/>
        <TextBox x:Name="BTextbox"/>
        <TextBox x:Name="CTextbox"/>
    </StackPanel>
    <StackPanel Grid.Column="1">
        <Button x:Name="AButton" Click="Button_Click" Content="Foo" Grid.Column="1"/>
        <Button x:Name="BButton" Click="Button_Click" Content="Foo" Grid.Column="1"/>
        <Button x:Name="CButton" Click="Button_Click" Content="Foo" Grid.Column="1"/>
    </StackPanel>
</Grid>

As you can see, I would really like to have a single "click" method to handle all three buttons. I would like the C# to look something like the following:

private void Button_Click(object sender, EventArgs e) 
{
    //Get the name of the button clicked
    DependencyObject dpsender = sender as DependencyObject;
    string name = dpsender.GetValue(FrameworkElement.NameProperty) as string;
    string subName = name.Substring(0, 1);
    string tbName = subName + "Textbox";
    string text = "Calculated text";

    tbName.Text = text;
}

I essentially get the name of the button clicked, grab the prefix (A, B, or C), then concatenate the prefix and "Textbox" to get the name of textbox I am wanting put the data in.

The last line obviously doesn't work, because tbName is a string, which does not have a "Text" property. However, I know that at runtime the name of a Textbox object will be contained in the tbName variable. Is there any way to achieve what I am trying to do? I know I could obviously create separate methods for each button, but I would rather avoid this if possible.

Thanks

  • 4
    Coding in WPF this way is a very bad idea. With this kind of approach, you'll be fighting WPF every step of the way, and after a long and bloody conflict it will usually win. I suggest you use MVVM and data binding. – 15ee8f99-57ff-4f92-890c-b56153 Apr 25 '18 at 19:24
  • https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type I think you meant to ask this question. – DoomVroom Apr 25 '18 at 19:26
  • FrameworkElement.FindName https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname.aspx – Kevin Apr 25 '18 at 19:38

1 Answers1

1

You should learn mvvm.

In the meantime, abuse the tag on your buttons and bind that to the textbox you're interested in. Get a reference to them by grabbing it back out.

Something like:

<Button x:Name="AButton" Tag="{Binding ElementName=ATextBox}"

and

private void Button_Click(object sender, EventArgs e) 
{
    //Get the name of the button clicked
    Button btn = sender as Button;
    ((TextBox)btn.Tag).Text = "Calculated text";
}

This is just air code and may have typos.

Andy
  • 11,864
  • 2
  • 17
  • 20