我正在使用xamarin表单网格,当点击网格按钮时我有一个事件处理程序按钮我不知道如何知道按钮在哪一行和哪一行。
这是我的网格代码:
<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>
和eventHandler代码:
void handle_clicked(Object sender, System.EventArgs e)
{
Button thebutton = (Button) sender;
int row = ?;
int column = ?;
//Display picker view with options
//Displaypickerview();
}
感谢您的帮助。
更新:
你的答案有效,但为什么Grid大写。
因为在我的网格代码中我有网格名称小写,所以我认为你会使用小写,所以你搜索我的网格中的按钮。
答案 0 :(得分:9)
试试这个:
Button thebutton = (Button) sender;
int row = Grid.GetRow(theButton);
int column = Grid.GetColumn(theButton);
答案 1 :(得分:0)
关于你的问题,为什么它与资本网格。
静态方法是非基于实例的方法。他们直接来自班级本身。在这种情况下,这个类叫做“网格”。
如果您不理解类vs instance vs objects的概念,请阅读:
https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/
这是我能找到的最简短的解释: