Accessibility of Window components

hi all,
i have a window 'mainwindow', with its components in its xaml file. this window contains a textbox, which i want to access from another class. 'mainwindow' is initialized at program start, so i think there has to be an object of this class available for another class.

but if i try to do something like this:

mainwindow_textboxname.Text = "sometext";

i get the following error msg.:
'An object reference is required for the nonstatic field, method, or property'


can anyone help to solve this problem

thanks in advance...


Answer this question

Accessibility of Window components

  • Senkwe Chanda

    hello joseph,
    thanks for your reply.
    i get no compiler error. :-(
    i mailed you the code and hope you can help me out...

    thanks in advance,
    .p

  • LisaAnn

    What is the compiler error Rather than the getter for Instance that you've added to Main it might be better to pass the instance to the type defined in Communication.cs, otherwise you might be (inadvertently) creating another Main window. If you like you can email me the code to joseph(dot)cooney(at)gmail(dot)com.

  • erudite_22

    Hi Kalinkula

    WPF objects are essentially just regular .NET objects so the same rules for accessing member variables applies. In order to set the text on the textbox property you would need an instance of the mainwindow class, and the textbox would need to be accessible (declared public or internal in C#). The slightly tricky thing here is that the declaration for the textbox in the mainwindow class is not written by you, it is in the class generated from the XAML and I couldn't see any way you could modify the accessibility via XAML. Windows Forms exposes this via a property called "Modifiers" which controls how the C# partial class generated by the designer is declared (either public, private etc). In WPF we can get around this by writing a property wrapper around the textbox. This is probably a good practice anyway, rather than exposing the member variables directly. The code to expose the textbox on mainwindow would look something like this:

    public TextBox MainWindowTextBox
    { get { return textBox1; } }

    And then you would set the text property like so once you have an instance of your mainwindow class (which mainwindow can pass to others).

    instanceOfMainWindow.MainWindowTextBox.Text = "You clicked the button";

    I'd be interested in knowing if there is another way to control the accessibility via  XAML.

  • rafarebuscando

    hi there,
    thanks for your help. i tried it, like you sayed, but it didnt work...i dont get an error when compiling the project, but the textbox doesnt show any text either..  :-(

    what i did, was the following:

    i added in 'Main.xaml.cs':

    public TextBox myTB
            {
                get { return tb_status; }
            }

    ...where tb_status is the 'original' textboxname in the xaml...
    and:

    public static Main Instance
            {
                get
                {
                    if (_instance == null)
                        _instance = new Main();

                    return _instance;
                }
            }

    ..to have an instance of this class...

    and then i expansed the class 'Communication.cs', in which i declared a method to update the textbox in mainwindow with:


    public void updateStatus(string status)
            {
            Main.Instance.myTB.Text = status;
            }


    i have no clue right now, what i did wrong...maybe you

  • Lambros Vasiliou

    are you sure that you are getting the actual instance of Main() that is showing on the screen my guess is that you are creating a new instance of Main() that is not visible. look in your Application.xaml.cs file (or wherever the real main window is created) and use *that* instance.


  • Accessibility of Window components