Solved

WPF Update a user control embedded into my window

Posted on 2016-09-19
3
37 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
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 …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 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