0

Relative noob here coming from Python trying to build a UWP app in c# and XAML.

The short version: How do I refer to a button using its string 'Name' to change its color. It seems that if I have given it a name I should be able to refer to it by its name, something like:

"navBtn5".Background = myColor;
//or
navBtn5.Background = myColor;

I have looked all over, the best I found was this from here on stackOverflow: How to control a xaml button from C# in WPF

which looks exactly like what I want. However this does not work, will not even build as I just get told:

Error CS0103 The name 'navBtn5' does not exist in the current context

This could be because I am using UWP and that is for WPF.

The long Version: My app is creating the navigation menu by data binding to a List that is put together in a separate class. I am creating a type of MenuButton in my class then returning a list of them. This is a stripped down version of my class:

class MenuButton{
    public string buttonName { get; set; }
    public string buttonContentText { get; set; }
    public int buttonWidth { get; set; }
    public int buttonHeight { get; set; }
    public List<byte> buttonColor { get; set; }        
}


class menuButtonManager{
    public static List<MenuButton> getButtons(){

        var menuButtons = new List<MenuButton>();

        menuButtons.Add(new MenuButton{
            buttonName = "navBtn1",
            buttonWidth = 50,
            buttonHeight = 50,
            buttonContentText = "\uE173",
            buttonColor = new List<byte> { 255, 82, 190, 128 },
        });
        menuButtons.Add(new MenuButton{
            buttonName = "navBtn2",
            //and so on
        });
        menuButtons.Add(new MenuButton{
            //and so on
       });
    }

    return menuButtons;
}

At Run Time, when the user clicks a button I want to loop through all the buttons in the List and change their color to grey if it was not that one that was clicked, green if it was the clicked button. something like this:

private void navClick(object sender, RoutedEventArgs e){

    //who called me
    string senderName = ((Button)sender).Name;

    foreach(MenuButton m in menuButtons){

        if(m.buttonName == senderName){
            Button senderButton = (Button)sender;
            senderButton.Background = myFunkyColor;

            // I can already do this as I already have
            // the sender cast to a button

        }
        else{
            // change button to grey
            // This is where I am stuck
            // I know m.buttonName as a string
            // how do I create a usable button object from it?
        }
    }
}

As you can see I have no trouble changing the properties of the button that was the sender as I have cast it to a type of Button which I can use. The problem for me is when I am acting on a button that is not the sender and all I have is it's string name.

Community
  • 1
  • 1
chris
  • 1
  • 3
  • in your `else`, have you tried to cast the `MenuButton m` in to a `Button` and then change its `Background`? – bit Jul 19 '16 at 09:35

1 Answers1

0

You do not have to create a new class if you are using default properties(Unless it is your requirement).

Make your List of Buttons Global

List< Button> menuButtons = new List< Button>();

public static List<Button> getButtons()
{
    for ( int i=0; i<5; i++)
    {
        Button _button = new Button
        {
            Name = "navBtn" + i.ToString(),
            Width = 50,
            Height = 50,
            Content = "\uE173",
            Background = new SolidColorBrush(Colors.Gray)
        };
        _button.Click += _button_Click;
        menuButtons.Add(_button);
    }
    return menuButtons;
}

The above will return 5 new buttons( something like your requirement ). Look at

_button.Click += _button_Click;

An event for click will be explicitly created for each button in the list.

private static void _button_Click(object sender, RoutedEventArgs e)
{
    Button Clicked = (Button)sender;
    foreach(Button btn in menuButtons)
    {
        if (Clicked.Name == btn.Name)
        {
             btn.Background = myFunkyColour;
        }
        else
        {
            btn.Background = //Your Disabled Colour.
        }
    }
}

Click event will return you the button that was clicked.

Hope this helps.

AVK
  • 3,893
  • 1
  • 22
  • 31
  • Thanks, that does look a lot more elegant that my code. However, the problem I have is changing properties on all the buttons that were not clicked. So as in your example above how would I loop through and edit the other buttons given that I have their names from the loop – chris Jul 19 '16 at 07:36
  • I modified my answer. See above – AVK Jul 19 '16 at 09:27
  • Thanks. I'll check it out and see what I can get. – chris Jul 20 '16 at 09:38
  • I was creating a new class because I was building a hamburger menu and the stuff in the new class also has the text and information for some text boxes that accompany the buttons. But maybe in trying to simplify things I have inadvertently over complicated it – chris Jul 20 '16 at 09:40
  • Did you see Template 10? https://github.com/Windows-XAML/Template10/wiki/Docs-%7C-Controls – AVK Jul 20 '16 at 10:19
  • Hi, I did not see that yet, thank you I will take a look at it – chris Jul 25 '16 at 11:40