Search code examples
c#windows-phone-8windows-phone-8.1

Windows phone 8.1 button click arguments


Is there a way to pass arguments on button click in Windows Phone 8.1?

I have a grid of 5x5 buttons, and they should all call the same method but with a different parameter. I am adding a handler like this:

foreach (var child in buttonGrid.Children)
{
    Button b = child as Button;
    if (b != null)
    {
        b.Click += Button_Click;
        // I want to add an argument to this
    }
}

Now the only way I can get the index of the button is by iterating over the whole grid and checking if the sender is equal to the button:

private void Button_Click(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < buttonGrid.Children.Count; i++)
    {
        if (sender == buttonGrid.Children[i])
        {
            DoSomething(i);
            return;
        }
    }
}

It works, but I don't really like this approach. Is there a more efficient way of doing this (other than creating a different method for each of the 25 buttons)?

I tried searching on the internet, but the documentation and examples for Windows Phone are really lacking. If anyone has a good repository of Windows Phone 8.1 tutorials to direct me to, that would also be of help.


Solution

  • You can use Tag property of the button.

    For eg.

    I'm trying to create a number pad which has 9 buttons with the respective number as the button content and i have set the same thing as the Tag property also.

    <StackPanel>
        <StackPanel Orientation="Horizontal" >
            <Button Content="1" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="1" />
            <Button Content="2" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="2" />
            <Button Content="3" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="3" />
        </StackPanel>
    
        <StackPanel Orientation="Horizontal">
            <Button Content="4" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="4" />
            <Button Content="5" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="5" />
            <Button Content="6" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="6" />
        </StackPanel>
    
        <StackPanel Orientation="Horizontal">
            <Button Content="7" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="7" />
            <Button Content="8" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="8" />
            <Button Content="9" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="48" FontWeight="Bold" Click="Button_Click" Tag="9" />
        </StackPanel>
    </StackPanel>
    

    This produces the following output :

    Output

    In your code behind you can now use the Tag property in the following manner

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var tag = (sender as Button).Tag;
    
        int t = Convert.ToInt16(tag);
    
        switch (t)
        {
            case 1:
    
                //Do Something
                break;
    
            case 2:
    
                //Do Something
                break;
    
            case 3:
    
                //Do Something
                break;
    
            case 4:
    
                //Do Something
                break;
    
            case 5:
    
                //Do Something
                break;
    
            case 6:
    
                //Do Something
                break;
    
            case 7:
    
                //Do Something
                break;
    
            case 8:
    
                //Do Something
                break;
    
            case 9:
    
                //Do Something
                break;
    
            default:
                break;
        }
    }