Ich arbeite mit einem Raster von Xamarin Formen, ich habe einen Event-Handler, wenn eine Schaltfläche des Grid angeklickt wird Button Ich weiß nicht, wie in welcher Zeile und Spalte zu wissen Knopf ist.Xamarin Grid, wie man weiß, welche Taste gedrückt wird
Das ist mein Netzcode:
<Grid x:Name="grid" ColumnSpacing="6" RowSpacing="5" RelativeLayout.WidthConstraint="{ConstraintExpression Type=RelativeToParent, Property=Width, Constant=-20}"
RelativeLayout.YConstraint="{ConstraintExpression Type=RelativeToParent,
Property=Height,
Factor=0.50 }">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="button1" Text="1" Grid.Row="0" Grid.Column="0" Clicked="handle_clicked"/>
<Button x:Name="button2" Text="2" Grid.Row="0" Grid.Column="1" Clicked="handle_clicked"/>
<Button x:Name="button3" Text="3" Grid.Row="0" Grid.Column="2" Clicked="handle_clicked"/>
<Button x:Name="button4" Text="4" Grid.Row="0" Grid.Column="3" Clicked="handle_clicked"/>
<Button x:Name="button5" Text="5" Grid.Row="0" Grid.Column="4" Clicked="handle_clicked"/>
<Button x:Name="button6" Text="6" Grid.Row="0" Grid.Column="5" Clicked="handle_clicked"/>
<Button x:Name="button7" Text="7" Grid.Row="1" Grid.Column="0" Clicked="handle_clicked"/>
<Button x:Name="button8" Text="8" Grid.Row="1" Grid.Column="1" Clicked="handle_clicked"/>
<Button x:Name="button9" Text="9" Grid.Row="1" Grid.Column="2" Clicked="handle_clicked"/>
<Button x:Name="button10" Text="10" Grid.Row="1" Grid.Column="3" Clicked="handle_clicked"/>
<Button x:Name="button11" Text="11" Grid.Row="1" Grid.Column="4" Clicked="handle_clicked"/>
<Button x:Name="button12" Text="12" Grid.Row="1" Grid.Column="5" Clicked="handle_clicked"/>
<Button x:Name="button13" Text="13" Grid.Row="2" Grid.Column="0" Clicked="handle_clicked"/>
<Button x:Name="button14" Text="14" Grid.Row="2" Grid.Column="1" Clicked="handle_clicked"/>
<Button x:Name="button15" Text="15" Grid.Row="2" Grid.Column="2" Clicked="handle_clicked"/>
<Button x:Name="button16" Text="16" Grid.Row="2" Grid.Column="3" Clicked="handle_clicked"/>
<Button x:Name="button17" Text="17" Grid.Row="2" Grid.Column="4" Clicked="handle_clicked"/>
<Button x:Name="button18" Text="18" Grid.Row="2" Grid.Column="5" Clicked="handle_clicked"/>
<Button x:Name="button19" Text="19" Grid.Row="3" Grid.Column="0" Clicked="handle_clicked"/>
<Button x:Name="button20" Text="20" Grid.Row="3" Grid.Column="1" Clicked="handle_clicked"/>
<Button x:Name="button21" Text="21" Grid.Row="3" Grid.Column="2" Clicked="handle_clicked"/>
<Button x:Name="button22" Text="22" Grid.Row="3" Grid.Column="3" Clicked="handle_clicked"/>
<Button x:Name="button23" Text="23" Grid.Row="3" Grid.Column="4" Clicked="handle_clicked"/>
<Button x:Name="button24" Text="24" Grid.Row="3" Grid.Column="5" Clicked="handle_clicked"/>
<Button x:Name="button25" Text="25" Grid.Row="4" Grid.Column="0" Clicked="handle_clicked"/>
<Button x:Name="button26" Text="26" Grid.Row="4" Grid.Column="1" Clicked="handle_clicked"/>
<Button x:Name="button27" Text="27" Grid.Row="4" Grid.Column="2" Clicked="handle_clicked"/>
<Button x:Name="button28" Text="28" Grid.Row="4" Grid.Column="3" Clicked="handle_clicked"/>
<Button x:Name="button29" Text="29" Grid.Row="4" Grid.Column="4" Clicked="handle_clicked"/>
<Button x:Name="button30" Text="30" Grid.Row="4" Grid.Column="5" Clicked="handle_clicked"/>
</Grid>
und Eventhandler Code:
void handle_clicked(Object sender, System.EventArgs e)
{
Button thebutton = (Button) sender;
int row = ?;
int column = ?;
//Display picker view with options
//Displaypickerview();
}
Danke für die Hilfe.
UPDATE:
Ihre Antwort funktioniert, aber warum ist Grid aktiviert.
Da ich in meinem Grid-Code Grid Name Kleinbuchstaben habe, dachte ich, Sie würden Kleinbuchstaben verwenden, so dass Sie die Schaltfläche in meinem Raster suchen.
Grid.GetRow/Grid.GetColumn statisch sind Methoden der Grid-Klasse: https://developer.xamarin.com/api/member/Xamarin.Forms.Grid.GetRow/p/Xamarin.Forms.BindableObject/. Der Name der Methoden steht in keinem Zusammenhang mit dem Namen Ihres aktuellen Rasters. – mm8
Ich finde es ein bisschen seltsam, aber oh, ich arbeite. – Cing