Avatar of cyimxtck
cyimxtck
Flag for United States of America

asked on 

WPF Update a user control embedded into my window

I am trying to update a user control in my windows and it doesn't work.  MSDN says that you have to use the UI thread to update it but with all the examples all over the place - nothing works...

I have a user control for a status which every window gets:

        public StatusBar()
        {
            InitializeComponent();
            //setInitialStatusBar();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = false;
            bw.RunWorkerAsync();
        }

        public void setStatusBar(ErrorValues ev)
        {

            tbxStatusBar.Text = ev.strErrorMessage;
            tbxStatusBar.TextWrapping = TextWrapping.Wrap;
            tbxStatusBar.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            tbxStatusBar.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
}

I am trying to update the status from a window which contains the text box in the XAML and the code behind is the same windows code behind:

        public void UsersOpen()
        {
            this.Show();
           
            var ev = new ErrorValues();

            ev.strMessageType = "BAD";
            ev.strErrorMessage = "HOLY CODE DICK TRACY!!";

            setStatus(ev);
        }

        [STAThread]
        private void setStatus(ErrorValues ev)
        {
            var stat = new StatusBar();
            Dispatcher.Invoke(() => Dispatcher.BeginInvoke((Action)(() => stat.setStatusBar(ev))));
        }


I have tried Invoke, BeginInvoke and dozens of other ways but this is very elusive.  So I am going to have 20 windows with this control that need to push messages to the control so it is displayed on the window.

Any help is greatly appreciated!
.NET ProgrammingC#

Avatar of undefined
Last Comment
Gautham Janardhan

8/22/2022 - Mon