如错误消息所示,您只能WPF
使用默认的无参数构造函数来实例化对象。所以你最好的选择是制作'Type' aDependencyProperty
并为其设置绑定,然后在设置时调用你的PopulateStudents()
方法。
public class StudentViewModel : DependencyObject
{
// Parameterless constructor
public StudentViewModel()
{
}
// StudentType Dependency Property
public string StudentType
{
get { return (string)GetValue(StudentTypeProperty); }
set { SetValue(StudentTypeProperty, value); }
}
public static readonly DependencyProperty StudentTypeProperty =
DependencyProperty.Register("StudentType", typeof(string), typeof(StudentViewModel), new PropertyMetadata("DefaultType", StudentTypeChanged));
// When type changes then populate students
private static void StudentTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var studentVm = d as StudentViewModel;
if (d == null) return;
studentVm.PopulateStudents();
}
public void PopulateStudents()
{
// Do stuff
}
// Other class stuff...
}
Xaml
<navigation:Page.DataContext>
<vms:StudentViewModel StudentType="{Binding YourBindingValueHere}" />
</navigation:Page.DataContext>