[英]How can I assign an id for radiobutton and access it at code behind in Xamarin Forms in order to not check both radiobuttons
xaml代碼
<input:RadioButton x:Name="button1" Clicked="Radio_Button_Clicked"
Grid.Column="1" Value="btn1"
Style="{StaticResource radiofontsize}">
</input:RadioButton>
<input:RadioButton x:Name="button2" Clicked="Radio_Button_Clicked"
Grid.Column="2" Value="btn2"
Style="{StaticResource radiofontsize}">
</input:RadioButton>
C#
private void Radio_Button_Clicked(object sender, EventArgs e)
{
}
您已經將StyleId
分配給您的按鈕,這是 XAML 中的x:Name
設置。
試試下面的代碼。 它會幫助你。
private void Radio_Button_Clicked(object sender, EventArgs e)
{
var styleId = (sender as RadioButton).StyleId;
if(styleId == "button1")
{
//Do your logic here for the button1 selection.
}
else
{
//Do your logic here for the button2 selection.
}
}
就像@Harikrishnan 所說,您也可以使用ClassId
。
<Button ClassId="btn1" x:Name="button1" />
<Button ClassId="btn2" x:Name="button2" Clicked="button2_Clicked"/>
private void button2_Clicked(object sender, EventArgs e)
{
var btn = sender as Button;
var id = btn.ClassId;
//.....
}
或者您可以使用FindByName
來獲取按鈕控件的名稱 id。
var s = this.FindByName<Button>("button1");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.