Solved

WPF Update a user control embedded into my window

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 35
C# Linq Select From List 3 42
parsing question on dynamic concatenation 3 33
C# Windows Form Navigation - Total Beginner 9 36
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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