在单击按钮的列表视图中传递数据

时间:2018-05-28 12:39:24

标签: listview xamarin.forms

我想将itemselected的数据传递给另一个页面。以下代码按预期工作。

private void inProgress_ItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            var surveyTapped = (SurveyList)e.SelectedItem;
            Navigation.PushAsync(new StartSurvey(surveyTapped.vchar_Title, surveyTapped.int_SurveyID, surveyTapped.int_UserSurveyID));            
        }

但是我在listview中有2个按钮,我必须添加点击这些按钮

My UI

private void resume_ButtonClicked(object sender, EventArgs e)
        {
            //How and what should i implement in order to pass the data 
        }

3 个答案:

答案 0 :(得分:2)

绑定是这里的救世主。

如果您要使用面向事件的方法,请尝试以下代码:

produces

答案 1 :(得分:1)

这就是我所做的对我来说完美无缺。我使用命令参数来传递值。这就是我在listview中所做的

<Button Text="Resume" TextColor="White" BackgroundColor="Green" HorizontalOptions="EndAndExpand" Clicked="resume_ButtonClicked" CommandParameter="{Binding .}"/>

和onclicked事件我给了按钮一个列表视图的引用。

private void resume_ButtonClicked(object sender, EventArgs e)
        {
            var menuItem = sender as Button;
            var selectedItem = menuItem.CommandParameter as SurveyList; 
            Navigation.PushAsync(new StartSurvey(selectedItem.vchar_Title, selectedItem.int_SurveyID, selectedItem.int_UserSurveyID));
        }

答案 2 :(得分:0)

尝试这个建议。

在向列表视图行添​​加按钮时,为它们添加唯一的“id”属性,这样您就可以知道单击了哪个按钮,它位于哪一行。

就像 - &gt; 第0行 - 按钮ID可以是01,02 第1行 - 按钮可以是10,11 等...... ..

因此,当您单击按钮时,您将知道单击了哪个按钮以及它在哪里。

当你知道listview行索引时,你可以从该索引的列表的itemsource属性中获取数据,或者使用你用来绑定数据的集合。

现在你有了数据,你可以传递你在第一种情况下所做的相同的事情。

这是我正在谈论的财产。

https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.element.id?view=xamarin-forms#Xamarin_Forms_Element_Id

相关问题