Rohit
Rohit

Reputation: 10236

Update label on MainWindow

I have a WPF application in which I am trying to update my wpf MainWindow from another window.

Here is my code.

MainWindow.xaml

   <Label x:Name="SomeLabel" Content="Label" Margin="290,2,0,0" VerticalAlignment="Top"  />

CallingWindow.xaml.cs

     Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                {    ((MainWindow)System.Windows.Application.Current.MainWindow).SomeLabel.Content ="Some Content";                        

                }));

However it is breaking at the point.The exception raised is

"Invalid Cast exception cannot cast object of CallingWindow to MainWindow"

What am I Missing ?

Upvotes: 0

Views: 579

Answers (2)

Philip Stuyck
Philip Stuyck

Reputation: 7447

If you want to have influence on MainWindow from another window, that means that the other window has a dependency towards MainWindow.
Make sure you pass MainWindow as a parameter to the constructor of the other window and store this dependency in a reference variable. ie _MainWindow
Then you can simply use that reference later on and use the dispatcher if you want this call to happen from a different thread than the GUI thread.
This is a far better solution than using any construct like : (MainWindow)System.Windows.Application.Current.MainWindow) It is also better to create a method on MainWindow and call that method instead of directly updating controls on mainwindow. This is better because then the other window does not have to know the details of mainwindow. Even better, you can create an interface for this updating method, and instead of passing the full mainwindow to the constructor of the other window you pass the interface.(See code sample below)

There are more alternatives like generating an event, but then you have to register mainwindow for the event of the producing window. You can move to MVVM but this will make for a more complicated design. I don't think you are using MVVM yet or you would not be asking this very question.;-).

Which solution is best depends on the situation. Your code is very small for us to make a good advice about it.
Just some example code to get you started it is far from complete:

public interface IUpdateTrigger{
    void updateNotification();
}

public class MainWindow : IUpdateTrigger{
    public void updateNotification(){
         //update controls, dispatch if needed
    }
    public void showSecondWindow(){
        SecondWindow second = new SecondWindow(this);
        //code to show the second window like second.show();
    }
}

public class SecondWindow{
    private IUpdateTrigger _UpdateTrigger;
    public SecondWindow(IUpdateTrigger trigger){
        _UpdateTrigger = trigger
    }
    public Whatevermethod(){
        _UpdateTrigger.updateNotification();
    }
}

Upvotes: 3

Alexander Bell
Alexander Bell

Reputation: 7918

In brief, that SomeLabel is not visible to the call from second window. To make a long story short, you can create some public static (or internal static) method to set the value of that Label; it should work even though it's not the most elegant solution (and, btw, it's better to use TextBlock control instead of Label). Hope this may help. Best regards,

Upvotes: 0

Related Questions