[英]object reference is required for the non-static field, method, or property
嗯,我似乎有问题,在我的主窗口上,我试图这样做:
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); //
}
在我的另一个窗口上,我有:
MainWindow.StudentID = (String)((Button)sender).Tag;
但是我得到了错误:
An object reference is required for the non-static field, method, or property 'WpfApplication4.MainWindow.StudentID.get'
有谁知道我该如何解决? 它适用于我的用户控件,但不适用于其他窗口吗?
我的主窗口实际上命名为MainWindow,所以我可能对此感到困惑。
您需要在MainWindow类的实例上设置StudentID。 尝试
((MainWindow)Application.Current.MainWindow).StudentID = (String)((Button)sender).Tag;
因为MainWindow
是类的名称,所以不是MainWindow的实例。 您需要类似:
MainWindow mw = new MainWindow();
mw.StudentID = (String)((Button)sender).Tag;
我试图从UserControl
更新MainWindow
的TextBox
并得到错误
Error 1: An object reference is required for the non-static field, method, or property
'WpfApplication1.MainWindow.textBox1'
我通过编写以下代码解决了该错误:
//MainWindow.textBox1.Text = ""; //Error 1
((MainWindow)Application.Current.MainWindow).textBox1.Text = "";//This is OK!
这是由Rytis I建议的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.