0

I have a WPF project in that I have multiple WPF windows.

In one of the window, I called it Window2 I have a text block as follow:

<TextBlock Grid.Row="0" Name="motorTitle" Text="this is test" Visibility="Visible" Foreground="Red" Grid.Column="0" FontSize="20" HorizontalAlignment="Center"></TextBlock>

In the code behind of Window2 file, I have the following:

public string text = ".";    
    private void timer_Tick(object sender, EventArgs e)
{
    this.motorTitle.Text = text;
}

In a different WPF window I called it MainWindow I try to update the text of Window2 by doing this

   Window2.text = "my text";

So, when I run in debug mode, I can see Window2's text variable changed to "my text" but it does not update to the textblock on the display.?

I did a try & catch but nothing found.

Any idea?

CB4
  • 690
  • 3
  • 13
  • 25
  • 2
    Show the code that creates and starts your timer. Also, have you set a breakpoint in the timer_Tick to verify it is being called? – user469104 Nov 21 '14 at 14:17
  • code that start timer is this public DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(300); timer.Tick += timer_Tick; timer.Start(); and yes, the breakpoint shows data in "text". – CB4 Nov 21 '14 at 14:35
  • You have to debug your code: is timer event occurs at all? Then tell what is `Window2`: is it an instance? Are you sure it's same instance (newbies often create `new` instance instead of using correct shown window one)? Also, I'd use `x:Name` instead of `Name`, don't ask me [why](http://stackoverflow.com/q/589874/1997232). – Sinatr Nov 21 '14 at 14:36
  • the timer event works. Windows2 is an instance created in MainWindow – CB4 Nov 21 '14 at 15:06
  • I see this error when I try to write motorTitle.text = text; "The calling thread cannot access this object because a different thread owns it." – CB4 Nov 21 '14 at 15:35
  • @user1801381 Use `Dispatcher.Invoke` to solve the cross-thread problem. http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.invoke%28v=vs.110%29.aspx – Ming Slogar Nov 21 '14 at 17:04

1 Answers1

1

You should probably use binding for the Textblock's text property and implement INotifyPropertyChanged, like this: http://msdn.microsoft.com/en-us/library/ms743695%28v=vs.110%29.aspx