Xamarin 表单:如何在列表视图中获取选定的选择器模型数据?
Xamarin forms: How to get the selected picker model data inside a listview?
我的列表视图中有一个选择器。我正在将数据从 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
}
]
}
选择器位于 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);
}
}
任何人都可以建议一种在列表视图中捕获所选选择器项目的方法吗?
提前致谢:)
Picker 的 ItemSource
是 List<string>
,因此您不能将其转换为 cbrainAttendanceHBList
。但是,选择器的 BindingContext
应该是该行的 cbrainAttendanceHBList
。
var picker = sender as Picker;
var selectedItem = picker.BindingContext as cbrainAttendanceHBList;
我的列表视图中有一个选择器。我正在将数据从 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
}
]
}
选择器位于 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);
}
}
任何人都可以建议一种在列表视图中捕获所选选择器项目的方法吗?
提前致谢:)
Picker 的 ItemSource
是 List<string>
,因此您不能将其转换为 cbrainAttendanceHBList
。但是,选择器的 BindingContext
应该是该行的 cbrainAttendanceHBList
。
var picker = sender as Picker;
var selectedItem = picker.BindingContext as cbrainAttendanceHBList;