如何在另一个按钮的点击事件中访问一个按钮中的发件人对象

本文关键字:按钮 一个 对象 事件 访问 另一个 | 更新日期: 2023-09-27 17:58:36

我正在开发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;
    }
}    

我在上面的方法中使用的逻辑对我来说没有任何意义,因为它允许我同时点击两个按钮。一次选择一个按钮需要进行哪些更改?

如何在另一个按钮的点击事件中访问一个按钮中的发件人对象

我自己解决了我的问题,如下所示-

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