none
Perform Click on button in a grid RRS feed

  • Question

  • Hello all.

    I am new to UWP/XAML but I've been using .NET for quite a while now. I have an issue as follows.

    I have an app which shows a Customer page. I've added a VariableSizedWrapGrid and called a sub to fill it with buttons as follows:

    <VariableSizedWrapGrid x:Name="FirstChars" Orientation="Horizontal"/>
                // Create first button '#' with character and Click event and add to grid
                ToggleButton btn = new ToggleButton { Content = '#', Margin = new Windows.UI.Xaml.Thickness(2, 0, 0, 0), Width = 34 };
                btn.Click += FirstChars_Click;
                FirstChars.Children.Add(btn);
                // Create all alphabets buttons and Click event and add to grid
                char[] az = Enumerable.Range('A', 'Z' - 'A' + 1).Select(i => (Char)i).ToArray();
                foreach (var c in az)
                {
                    btn = new ToggleButton { Content = c, Margin = new Windows.UI.Xaml.Thickness(2, 0, 0, 0), Width = 34 };
                    btn.Click += FirstChars_Click;
                    FirstChars.Children.Add(btn);
                }

    When the user enters this page, no buttons is selected and I want to perform a click event on the first button "A" so that I can then perform the appropriate Select query on my DB.

    How do I select the button and call the appropriate event when the page has been loaded?

    I'm guessing I need to define a ToggleButton variable and select the button from the grid which has the content "A". But I can't fathom how to do this after many tries :(

    Thank you for your help!

    Saturday, July 13, 2019 5:09 AM

All replies

  • Hi Jedimaster,
    If you can provide a more detailed description of the requirements, I think we can help you better.
    I have observed that all of your ToggleButtons are bound to the same event 'FirstChars_Click', but you also want to have special handling for different buttons (like pressing button A).
    Do you want to know which button the current trigger is when the event is triggered?
    Monday, July 15, 2019 5:45 AM
  • Thank you but I found the answer on another forum:

    After the foreach loop, add the following:

    this.Loaded += OnPageLoaded;

    private void OnPageLoaded(object sender, RoutedEventArgs e)
    {
        foreach (var item in FirstChars.Children)
        {
            if (item is ToggleButton button && button.Content.ToString().Equals("A"))
            {
                button.IsChecked = true;
                // Perform your query here or call the appropriate method
                break;
            }
        }
    }


    • Proposed as answer by Skittles2519 Thursday, July 25, 2019 6:01 AM
    Wednesday, July 17, 2019 7:00 AM
  • Hi,

    I am very happy that you have solved your problem and your answer will help more people.

    Thank you for your support.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 13, 2019 6:09 AM