简体   繁体   中英

How access sender object of one button in click event of another button

I am working on Xamarin.Forms and I have two buttons in my ListView and they are treated as radio button. On click event I am changing image property of button so that it works as a radio button. But in my case I need to have one button selected at one time. If user clicks on one radio button, other radio button should be automatically reset in order to only one button is selected at a time. I am sharing code of ListView here-

<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>

Also code behind for radio buttons click is here-

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;
    }
}    

The logic I used in above methods, not making any sense for me because it allows me to click both buttons simultaneously. What changes are required to make one button selected at a time?

I solved my problem by myself like this-

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  );
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM