Solved

WPF Update a user control embedded into my window

Posted on 2016-09-19
3
32 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
ID: 41805326
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
ID: 41805343
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
ID: 41805347
 <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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 105
How to prevent website from publishing with errors 9 46
What namespace do I need to import? 2 27
Moving code from one TFS branch to another 3 25
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 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