6

うーん、問題があるようです。メインウィンドウでこれを実行しようとしています。

    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という名前なので、これが混乱している可能性があります。

4

3 に答える 3

11

MainWindowクラスのインスタンスにStudentIDを設定する必要があります。試す

((MainWindow)Application.Current.MainWindow).StudentID = (String)((Button)sender).Tag;
于 2012-04-25T15:11:39.977 に答える
2

MainWindowこれはクラスの名前であり、MainWindowのインスタンスではないためです。次のようなものが必要です。

MainWindow mw = new MainWindow();
mw.StudentID = (String)((Button)sender).Tag;
于 2012-04-25T15:10:17.577 に答える
2

からを更新しようとしてTextBoxエラーが発生しましたMainWindowUserControl

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!

これはRytisIによって提案されました

于 2014-05-14T15:58:34.070 に答える