我正在研究Xamarin.Forms,我的ListView中有两个按钮,它们被视为单选按钮。在点击事件中,我正在更改按钮的图像属性,以便它作为单选按钮工作。但在我的情况下,我需要一次选择一个按钮。如果用户单击一个单选按钮,则应自动重置其他单选按钮,以便一次只选择一个按钮。 我在这里分享ListView的代码 -
<ListView RowHeight="35" SeparatorVisibility="None" BackgroundColor="{x:Static color:ColorResources.PageBackgroundColor}" HeightRequest="230" x:Name="listof_ingredients_and_lifestyleDiets">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Spacing="60" HorizontalOptions="FillAndExpand" Orientation="Horizontal">
<Label Text="dipak" FontSize="14" TextColor="{x:Static color:ColorResources.TextColor}" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<Button Image="radio_Check.png" Clicked="OnAllergenTapped" HeightRequest="25" WidthRequest="25" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<Button Image="radio_unCheck.png" Clicked="OnPreferenceTapped" HeightRequest="25" WidthRequest="25" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<Button Image="deleteBtn.png" Clicked="btnDeleteClicked" HeightRequest="50" WidthRequest="40" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
单击单选按钮的代码也在这里 -
public void OnAllergenTapped(object sender, EventArgs e)
{
var s = sender as Button;
if (flag_allergen == 1)
{
s.Image = "radio_uncheck.png";
flag_allergen = 0;
flag_preference = 1;
}
else
{
s.Image = "radio_Check.png";
flag_allergen = 1;
flag_preference = 0;
}
}
public void OnPreferenceTapped(object sender, EventArgs e)
{
var s = sender as Button;
if (flag_allergen == 1)
{
s.Image = "radio_uncheck.png";
flag_allergen = 0;
flag_preference = 1;
}
else
{
s.Image = "radio_Check.png";
flag_allergen = 1;
flag_preference = 0;
}
}
我在上面的方法中使用的逻辑,对我没有任何意义,因为它允许我同时点击两个按钮。一次选择一个按钮需要进行哪些更改?
答案 0 :(得分:0)
我自己解决了这个问题 -
Button btnPreference;
public void OnAllergenTapped(object sender, EventArgs e)
{
var allergen = sender as Button;
if(flag_allergen==1){
allergen.Image="radio_uncheck.png";
btnPreference = allergen.Parent.FindByName<Button> ("btnPreference");
btnPreference.Image = "radio_Check.png";
flag_allergen=0;
flag_preference=1;
}else{
allergen.Image = "radio_Check.png";
btnPreference.Image = "radio_uncheck.png";
flag_allergen=1;
flag_preference=0;
}
Debug.WriteLine ("Allergen="+flag_allergen+" Preference= "+flag_preference );
}