English 中文(简体)
非统计领域、方法或财产需要标注
原标题:object reference is required for the non-static field, method, or property
  • 时间:2012-04-25 15:06:43
  •  标签:
  • c#
  • wpf
  • linq

hmm 我似乎有一个问题,在我的主要窗口,我试图这样做:

    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,因此我可能已经混淆了。

最佳回答

你们需要为主妇dow学校树立学生知识。 提 出

((MainWindow)Application.Current.MainWindow).StudentID = (String)((Button)sender).Tag;
问题回答

因为MainWindow 是贵阶层的名称,而不是主妇dow的例子。 你们需要这样的东西:

MainWindow mw = new MainWindow();
mw.StudentID = (String)((Button)sender).Tag;

我试图从<代码>UserControl上更新

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建议这样做。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签