简体   繁体   English

Xamarin,使用 x:name 找不到另一个按钮

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

I'm having some problems on finding another button in my ListView.我在 ListView 中查找另一个按钮时遇到了一些问题。

Here's the XAML:这是 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.我想要做的是,如果我单击 YesButton,YesButton 的边框颜色会更改,并且它也会更改 NoButton 边框颜色。

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:我试图通过在这样的代码中调用另一个按钮来使用 X:name:

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对每个是/否按钮使用这些events

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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