【问题标题】:Modifying the object behind ListView, when clicking a button单击按钮时修改 ListView 后面的对象
【发布时间】:2020-06-14 22:23:29
【问题描述】:

我有一个相当简单的应用程序,它将用对象填充特定模块,称为:问题。该对象具有公共属性,如:String question {get;设置;} 和 int selectedAnswer{get;设置;}。

我想要实现的是,当用户按下 YesButton 或 NoButton 按钮时,会将该特定对象内的 selectedAnswer 更改为 0-2。在这种情况下,如果用户按下否,则该对象内部的值变为 1,如果是,则变为 2。默认为 0。

如何在 Clicked-eventhandler 中引用该对象?这是 XML 页面:

 <ContentPage.Content>

        <ListView ItemsSource="{Binding Modules}"
                  x:Name="QuestionsList_module"

                      HasUnevenRows="True"
                      Margin="10,10" >

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Margin="10, 20, 0, 10" >

                            <Label Text="{Binding name}"/>

                            <ListView ItemsSource="{Binding questionList}"
                                      x:Name="Item"
                                      HasUnevenRows="True"
                                      Margin="30,30">

                                <ListView.ItemTemplate>
                                    <DataTemplate>
                                        <ViewCell>

                                            <StackLayout Padding="0,10,0,10">
                                                <Label Text="{Binding question}"></Label>

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

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

                                                    </Grid.ColumnDefinitions>

                                                    <Button Margin="0,0,20,0" x:Name="YesButton" Text="YES" FontAttributes="Bold" BorderColor="LightGreen"  BorderWidth="5" CornerRadius="20" Clicked="YesButton_Clicked" />

                                                    <Button Margin="0,0,20,0" Grid.Column="1" x:Name="NoButton" Text="NO" FontAttributes="Bold" BorderColor="HotPink"  BorderWidth="5" CornerRadius="20" />

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

        </ListView>
    </ContentPage.Content> 

这是我迄今为止尝试获取发件人内部信息的尝试:

private void YesButton_Clicked(Object sender, EventArgs e)
        {

            var button = (Button)sender;

            Grid listview = (Grid)button.Parent;
            StackLayout ss = (StackLayout)listview.Parent;


        }

像这样,我可以获得上层标签,但仍然无法“访问”对象本身。

请注意,所有模块都是类型:ObservableCollection,所有问题对象也在 ObservableCollection 中,最终模型将如下所示:

new Module{
name = "this is the name of this module",
description = "Here goes the description",
isVisible = false,
ModuleIsChecked = false,
questionList = "And here's the list of those questions in ObservableCollection -list"}

【问题讨论】:

    标签: c# xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

    使用按钮的BindingContext获取绑定的对象,然后将其转换为正确的类型

    var button = (Button)sender;
    
    // cast to the appropriate class
    var item = (Question)button.BindingContext;
    

    【讨论】:

    • 谢谢!这成功了!男孩,我现在是否觉得自己很愚蠢,但显然我对 Xamarin 中的绑定还不够了解。回去学习!
    猜你喜欢
    • 1970-01-01
    • 2011-09-22
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多