0

I have a problem. I have to do app for college, guitar emulator, my idea was to put each fret as a button, and implement the color change function to understand which fret (button) is clamped (pressed)

var button = (Button)sender;
_buttonLastPressed = button;
var parent = (Grid)button.Parent;
var index = parent.Children.IndexOf(strip1_1);
var str = Math.Floor((decimal)(index + 1) / 20);
for (int i = 0; i < 20; i++)
{
    var element = (Button)parent.Children[i + Convert.ToInt32(str) * 20];
    element.Background = Brushes.White;
}
button.Background = Brushes.Blue;

This option works well, but the problem arose that I do not know how to get some information from the button in order to use it on what sound to play (so that the program understands which button is pressed). I believe that it was possible to somehow use the DataContext, but I cannot imagine in my head how to do it more competently

For the tip, there are six strings, I decided to declare each string as grid, and in each grid there are 20 frets (buttons), you can hold down only one of all 20

XAML:

<Button x:Name="strip1_1" Content="" HorizontalAlignment="Left" Margin="2,1,0,0" VerticalAlignment="Top" Width="22" Height="4" Click="Strip1_Click" BorderBrush="Black"/>
<Button x:Name="strip1_2" Content="" HorizontalAlignment="Left" Margin="37,1,0,0" VerticalAlignment="Top" Width="22" Height="4" Click="Strip1_Click" BorderBrush="Black"/>
Thomas Freudenberg
  • 5,048
  • 1
  • 35
  • 44
Axteity
  • 11
  • 2
  • Are you familiar with the `Command` property of `Button` and the `ICommand` interface? – Standard_101 May 26 '21 at 07:56
  • Could you also mention the XAML part of how you use the `` object? – Standard_101 May 26 '21 at 08:00
  • Never heard about it actually... – Axteity May 26 '21 at 08:01
  • @Maaz Oh... All 20 buttons in the grid are tied to one function, which is written in the question, so there are practically no differences between them. ` ` – Axteity May 26 '21 at 08:06
  • I would suggest, as a long term learning feature, reading up about MVVM pattern of programming. Get started here: [MVVM Pattern for Xamarin!](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/enterprise-application-patterns/mvvm) – Standard_101 May 26 '21 at 08:11
  • In the WPF paradigm, buttons should be bound to commands. You can include a command parameter so that the command being invoked knows which fret and or string is actually invoking the command See duplicate for details. – Peter Duniho May 26 '21 at 08:18
  • @Maaz thank you so much for help, appreciate that a lot <3 – Axteity May 26 '21 at 09:25

1 Answers1

0

A possible solution could be to use the Click property of each button which references a different method.

<Button Click="DoSomething_OnClick"/>

and in the code-behind:

private void DoSomething_OnClick(object sender, RoutedEventArgs e)
{
    // Your code here
}

You could use this to have 20 different methods. This way, you know exactly which button called which method.

Standard_101
  • 325
  • 4
  • 14