Xamarin表单:如何在列表视图中获取选定的选择器模型数据?

时间:2019-06-27 12:20:26

标签: listview xamarin.forms picker

我的Listview中有一个选择器。我将数据从REST API调用绑定到listview。以下是我的模型:

public class Attendance
{
  public List<cbrainAttendanceHBList> cbrainAttendanceHBList { get; set; }
}

public class cbrainAttendanceHBList
{
   public string userId { get; set; }
   public string name { get; set; }
   public string isPresent { get; set; } 
   public string status
    {
        get
        {
            if (isPresent == "1.0")
                return "Present";
            else if (isPresent == "0.0")
                return "Absent";
            else if (isPresent == "0.5")
                return "Half Day";
            else return "";
        }
    }
}

Json示例:

{  
   "cbrainAttendanceHBList":[  
      {  
         "userId":11709,
         "name":"zzzz",
         "isPresent":0.0,
         "date":1561637200408
      },
      {  
         "userId":11702,
         "name":"xxxx",
         "isPresent":1.0,
         "date":1561637200408
      },
      {  
         "userId":11699,
         "name":"yyyy",
         "isPresent":0.5,
         "date":1561637200408
      }
   ]
}

选择器位于ListView的内部。选择器代码:

 <ListView 
       x:Name="StudentList"
       RowHeight="75"
       BackgroundColor="White"
       HasUnevenRows="True">  
       <ListView.ItemTemplate>
           <DataTemplate>
                 <ViewCell>
                       <ViewCell.View>
                            <StackLayout 
                                  HorizontalOptions="FillAndExpand"
                                  VerticalOptions="FillAndExpand"
                                  Margin="5"
                                  Padding="5"
                                  Orientation="Horizontal">

                                  <Label 
                                        Text="{Binding name}"
                                        Font="17" 
                                        TextColor="#474747"
                                        HorizontalOptions="Start" 
                                         VerticalOptions="Center"/>

                                   <Picker 
                                         HorizontalOptions="EndAndExpand"
                                         Margin="0,0,20,0"
                                         SelectedItem="{Binding status}"
                                         SelectedIndexChanged="AttendanceStatus"
                                         WidthRequest="100"
                                         VerticalOptions="CenterAndExpand"
                                         TextColor="#5abcd7"  
                                         HeightRequest="50">
                                         <Picker.ItemsSource>
                                              <x:Array Type="{x:Type x:String}">
                                                  <x:String>Present</x:String>
                                                  <x:String>Half Day</x:String>
                                                  <x:String>Absent</x:String>
                                                </x:Array>
                                           </Picker.ItemsSource>
                                     </Picker>
                             </StackLayout>
                         </ViewCell.View>
                     </ViewCell>
                  </DataTemplate>
               </ListView.ItemTemplate>
            </ListView>

在选择器中更改选项时,我需要获取当前的列表视图项,我尝试使用SelectedIndexChanged属性。但总是为selectedItem获取空值。

以下是我的代码:

private void AttendanceStatus(object sender, EventArgs args)
        {
            try
            {
                var item = sender as Picker;
                var selectedItem = item.SelectedItem as cbrainAttendanceHBList;
                if(selectedItem != null)
                {
                    Debug.WriteLine("name:>." + selectedItem.name);
                }
            }
            catch(Exception e)
            {
                Debug.WriteLine("Exception:>>"+e);
            }
        }

有人可以建议一种在列表视图中捕获所选选择器项目的方法吗?

先谢谢您了:)

1 个答案:

答案 0 :(得分:2)

选取器的ItemSourceList<string>,因此您不能将其转换为cbrainAttendanceHBList。但是,选择器的BindingContext应该是该行的cbrainAttendanceHBList

var picker = sender as Picker;
var selectedItem = picker.BindingContext as cbrainAttendanceHBList;