【问题标题】:Xamarin forms: How to get the selected picker model data inside a listview?Xamarin 表单:如何在列表视图中获取选定的选择器模型数据?
【发布时间】:2019-06-27 12:20:26
【问题描述】:

我的列表视图中有一个选择器。我正在通过 REST API 调用将数据绑定到列表视图。以下是我的模型:

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
      }
   ]
}

Picker 位于 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);
            }
        }

谁能建议一种在列表视图中捕获所选选择器项的方法?

提前谢谢:)

【问题讨论】:

    标签: listview xamarin.forms picker


    【解决方案1】:

    选择器的ItemSourceList&lt;string&gt;,因此您不能将其转换为cbrainAttendanceHBList。但是,Picker 的 BindingContext 应该是该行的 cbrainAttendanceHBList

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多