非静态字段,方法或属性需要对象引用

Kir*_*ite 3 c# linq wpf

嗯我好像有问题,在我的主窗口我试图这样做:

    public static readonly DependencyProperty StudentIDProperty = DependencyProperty.Register("StudentID", typeof(String), typeof(LoginWindow), new PropertyMetadata(OnStudentIDChanged));

    public string StudentID
    {
        get { return (string)GetValue(StudentIDProperty); }
        set { SetValue(StudentIDProperty, value); }
    }

    static void OnStudentIDChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        (d as LoginWindow).OnStudentIDChanged(e); // 
    }
Run Code Online (Sandbox Code Playgroud)

在我的另一个窗口,我有这个:

MainWindow.StudentID = (String)((Button)sender).Tag;
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

An object reference is required for the non-static field, method, or property 'WpfApplication4.MainWindow.StudentID.get'
Run Code Online (Sandbox Code Playgroud)

有谁知道我怎么解决这个问题?它适用于我的用户控件,但不适用于其他窗口?

我的主窗口实际上名为MainWindow,所以我可能有这个困惑.

Ryt*_*s I 8

您需要在MainWindow类的实例上设置StudentID.尝试

((MainWindow)Application.Current.MainWindow).StudentID = (String)((Button)sender).Tag;
Run Code Online (Sandbox Code Playgroud)