0

I am building an interface in WPF. My MainWindow class has access to the TextBlock that I created in the window. It can write text to this TextBlock by just calling the textblock.text method.

I then created a second class, that is instantiated immediately when the program starts. This class has a method I created that cannot see that textblock. How would I be able to access that .text method that is accessible in MainWindow?

Thanks

MainWindow.xaml.cs

    {

        ButtonFunctions buttonFunctions = new ButtonFunctions();


        public MainWindow()
        {
            InitializeComponent();
            ProgressBar ProgBar = new ProgressBar();
        }
//this works
textBlock.Text = "r";

ButtonFunctions.cs

    {

        public ButtonFunctions()
        {

        }

        public void Addtext(string t)
        {
            //this doesn't work
            textBlock.Text += t;
            //this doesnt work either
            MainWindow.textBlock.Text="r";
        }

If I try to call the MainWindow method, I get the below error. An object reference is required for the non-static field, method or property "MainWindow.textBlock.Text" What object reference is needed in this case?

  • You should probably add the `WPF` tag – Ňɏssa Pøngjǣrdenlarp Jul 25 '19 at 23:44
  • May be you can pass textBlock into Addtext method from MainWindow, then you can use it within that scope. – estinamir Jul 25 '19 at 23:48
  • The fundamental issue is you're trying to access a non-static member without providing an object reference. See marked duplicate for the details you should have found when you searched Stack Overflow for the error message. That said, this is coming up because of the broader problem with your code: you are manipulating properties of UI objects directly, instead of using MVVM as WPF was designed to be used. You then compound this design problem as described in the answer you posted below, by assuming there is just one `MainWindow` object and allowing random code to modify the text directly. – Peter Duniho Jul 29 '19 at 04:54

2 Answers2

0

You're trying to reference MainWindow.textBlock as if it were static. You need to reference an existing instance of your window MainWindow. Secondly, you probably won't be able to set the value from another thread/context. I don't know WPF well but in Windows Forms you need to invoke the accessor. Maybe look at this Invoke WPF Setter manually

Zakk Diaz
  • 1,063
  • 10
  • 15
0

thanks for your responses. I finally had time today to sit down and try out a few changes to find a solution. I've settled on one thing that works, although might not be the best.

I've moved the Addtext method back to MainWindow.xaml.cs, and I have statically created a MainWindow object called AppWindow which I then reference from other classes.

For example in my ButtonFunctions class, I call MainWindow.AppWindow.Addtext("text to change"); to now put text in the textblock of the main window. Below is a big more code so you can reference and use it in case this is a problem for others.

public partial class MainWindow : Window
       {
        ButtonFunctions buttonFunctions = new ButtonFunctions();
        public static MainWindow AppWindow;

        public MainWindow()
        {
            InitializeComponent();
            ProgressBar ProgBar = new ProgressBar();
            AppWindow = this;
        }
        public void Addtext(string t)
        {
            textBlock.Text += t;
        }
       }

Then in another class, I just invoke this method.

MainWindow.AppWindow.Addtext("\nConnection to server started...\n");

Hope this helps anyone with the same problem.