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>