Solved

WPF Update a user control embedded into my window

Posted on 2016-09-19
3
29 Views
Last Modified: 2016-09-19
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!
0
Comment
Question by:cyimxtck
  • 2
3 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
Comment Utility
looks like you are setting on a new object (var stat = new StatusBar();)

if its in same code behind as user contrl

             Dispatcher.Invoke(() => Dispatcher.BeginInvoke((Action)(() => this.setStatusBar(ev))));

Open in new window



if the code behind is ur window where user control is embedded then replace "this" with the name of the control in xaml
0
 

Author Comment

by:cyimxtck
Comment Utility
There are two different code behinds/classes.....when I say same page I mean in the XAML:

        <StackPanel Margin="0,0,0,0">
            <local:StatusBar Height="Auto" Margin="10,10,10,411"/>
        </StackPanel>

The above code of course wouldn't work because this.setStatusBar = var stat = new StatusBar(); as you mentioned.

Thanks for the quick reply.  Do you have any other ideas?
0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 total points
Comment Utility
 <StackPanel Margin="0,0,0,0">
            <local:StatusBar x:Name="stausBar" Height="Auto" Margin="10,10,10,411"/>
        </StackPanel>

Open in new window


and then change code to

             Dispatcher.Invoke(() => stausBar.setStatusBar(ev));

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now