【发布时间】:2019-10-01 05:52:20
【问题描述】:
我正在使用 Prism MVVM 将 ItemSource 绑定到 ListView。这是列表Cell
<ListView x:Name="list1" ItemsSource="{Binding StudentList}">
<ListView.ItemTemplate >
<DataTemplate>
<ViewCell>
<StackLayout>
<Label VerticalTextAlignment="Center"
Text="{Binding StudentName}" />
<Button x:Name="mybtn"
BindingContext="{Binding Source={x:Reference list1}, Path=BindingContext}"
Command="{Binding BtnTextCommand}"
CommandParameter="{Binding Source={x:Reference mybtn}}"
Text="{Binding BtnText}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是ViewModel中的来源
private List<Student> _studentList;
public List<Student> StudentList
{
get { return _studentList; }
set { SetProperty(ref _studentList, value); }
}
我想在绑定之前每次为每个列表项修改 Button Text。如何从 ViewModel 类中获取 BtnText 的更新值?
修改值意味着如果Text 的ID 为value,则显示'Present',如果为null,则显示'Absent' 等等。
学生班
public class Student
{
public Mychildren MyKid { get; set; }
public string LocalStudentId { get; set; }
public int? StudentId { get; set; }
public string StudentName { get; set; }
public int? AttendanceTypeStatusId { get; set; }
public int? StudentDailyAttendanceId { get; set; }
public int? AbsentReasonId { get; set; }
public string AttendanceTypeStatusCD { get; set; }}
}
编辑:
我已经在 Converter 中编写了这个逻辑,在else 的第一个value 部分抛出异常
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string attStatusId = string.Empty;
//AttendanceTypeStatusId
if (value != null)
{
attStatusId = "Present";
}
else
{
attStatusId = SISConst.AttendanceResults.FirstOrDefault(i => i.AttendanceTypeStatusID == System.Convert.ToInt32(value)).AttendanceTypeStatusCD;
}
if (attStatusId == "Absent")
{
return "A";
}
else if (attStatusId == "Present")
{
return "P";
}
else
{
return "L";
}
}
异常截图
【问题讨论】:
-
也许创建一个名为 BindableModel 的新模型并使用此模型填充 ListView,在该模型中根据您的条件分配值?
-
ButtonTextHold的目的是什么?在更改ButtonTextHold时,将其删除或将PropertyChanged提高为ButtonText。
标签: c# xamarin mvvm xamarin.forms prism