0

I have the following dispatch routine in VS2013 C#:

private void B_Click(object sender, RoutedEventArgs e)
{
    Button btn = (Button)sender;
    string src = btn.Name.ToString();
    string foo = "G" + src.Substring(1);

    G0.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}

It currently changes the visibility of G0. I want to change the code so that if Button B123 is pressed, then G123.Visibility is changed.

Thanks, Dan

Note: This is a generic eventhandler for the buttons. There are 100's of buttons so an individual handler for each button is not practical. It could also be the handler from a dropdown or text box. G123 is a random control on the XAML page. The point is, given a string that contains the Name, how do I find the associated control so that I can modify its properties?

Dan Dickerson
  • 43
  • 2
  • 9
  • Create an eventhandler for your button and write the code? – Matthijs Jun 12 '14 at 06:22
  • Use [FindName](http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname.aspx). – Clemens Jun 12 '14 at 06:58
  • So if I understand correctly, when the user presses a button labeled Bxxx you want to change visibility of the corresponding control Gxxx, where xxx is a number associated with the control name. Why can't you just bind the each visibility in xaml? Or were you just hoping for a centralized location? – GEEF Jun 12 '14 at 13:54

2 Answers2

0

I'm not sure that I correctly understand your question, so I may be guessing that if button buttons B123 and G123 are related to each other by the number 123. In general, I suppose you want to change the visibility of button GX if button BX is changed. In order to find all controls in the Window, have a look at the solution provided by Bryce Kahle, see Find all controls in WPF Window by type. In your buttenclick handle, do something like

private void B_Click(object sender, RoutedEventArgs e)
{
   Button btn = (Button)sender;
   string src = btn.Name.ToString();
   string identifier= src.Substring(1);

   foreach (var btn in FindVisualChildren<Button>(this).Where(b => b.Name.EndsWith(identifier)))
   {
      if(btn.Name.StartsWith("G"))
        btn.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
   }
}

Hope that helps.

Community
  • 1
  • 1
SimonAx
  • 1,176
  • 1
  • 8
  • 32
0

In the comments, user Clemens gave the answer. (Since he didn't give it as an answer, I can't vote it up.)

Using FindName, I was able to get to the properties of the desired control:

private void B_Click(object sender, RoutedEventArgs e)
{
    Button btn = (Button)sender;
    string src = btn.Name.ToString();
    string foo = "G" + src.Substring(1);
    Windows.UI.Xaml.Shapes.Rectangle rect = (Windows.UI.Xaml.Shapes.Rectangle)this.FindName(foo);
    rect.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}

This has the flexibility so that I can change the fill, contents, text, foreground, style, etc. for a specified control. More control than if I had simply used XAML binding.

Thanks Clemens,

Dan

Dan Dickerson
  • 43
  • 2
  • 9