简体   繁体   中英

Xamarin, Can't find another button using x:name

I'm having some problems on finding another button in my ListView.

Here's the XAML:

<ListView ItemsSource="{Binding QuestionList}"
                                      x:Name="Item_list"
                                      HasUnevenRows="False"
                                      IsVisible="{Binding IsVisible}"
                                      HeightRequest="5400"
                                      RowHeight="180"
                                      BackgroundColor="#BED6E1">

                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <ViewCell>
                                                <StackLayout BackgroundColor="#BED6E1" HorizontalOptions="Center" VerticalOptions="Center">
                                                    <Label Text="{Binding question}" FontSize="16" Padding="10,20,10,10"/>

                                                    <Grid HorizontalOptions="Center" Padding="15,20,0,10">
                                                        <Grid.ColumnDefinitions >

                                                            <ColumnDefinition Width="*" />
                                                            <ColumnDefinition Width="*" />

                                                        </Grid.ColumnDefinitions>


                                                        <Button x:Name="YesButton" Margin="0,0,20,0" Text="YES" FontAttributes="Bold" BorderWidth="2" BorderColor="White" CornerRadius="10" Clicked="YesButton_Clicked" ClassId="YesButton" BackgroundColor="White" />

                                                        <Button x:Name="NoButton" Margin="0,0,20,0" Grid.Column="1" Text="NO" FontAttributes="Bold" BorderWidth="2" BorderColor="White" CornerRadius="10" BackgroundColor="White" Clicked="NoButton_Clicked"  ClassId="NoButton"/>


                                                    </Grid> 
                                                </StackLayout>
                                            </ViewCell>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>

What I'd like to do, is that if I click the YesButton, the YesButton's border color changes, and it changes the NoButton border color as well.

I can reference the button clicked like this:

private void YesButton_Clicked(Object sender, EventArgs e)
        {
            var button = (Button)sender;
            button.BorderColor = Xamarin.Forms.Color.FromRgb(33, 255, 20);

... (and here's some other stuff)
        }

But how can I reference the other button at the same time? All in all, if the user clicks Yes-button, the border color changes to that button and it will "reset" the border color for the No-button.

Please note, that there is a lot of these yes-no -buttons in a list.

I have tried to use X:name, by calling the other button in a code like this:

NoButton.BorderColor = Xamarin.Forms.Color.FromRgb(33, 255, 20);

But it doesn't find it and returns an error. Any ideas?

Use these events for every Yes/No Button

private void Yes_Clicked(object sender, EventArgs e)
{
   (sender as Button).BorderColor = Xamarin.Forms.Color.FromRgb(33, 255, 20);
   Button NoButton = (sender as Button).Parent.FindByName<Button>("NoButton");
   NoButton.BorderColor = Xamarin.Forms.Color.FromRgb(31, 78, 123); 
}

private void No_Clicked(object sender, EventArgs e)
{
   /* Same logic */
   Button YesButton = (sender as Button).Parent.FindByName<Button>("YesButton");
}

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