Oslo Young
Oslo Young

Reputation: 31

How to pass a parameter in wpf C#

I have a code in WindowAfterLogin.xaml:

<TextBlock x:Name="In_Time" Foreground="#FF55534F" 
           FontSize="71.312" FontFamily="HelveticaNeueCyr" 
           Height="79.45" LineStackingStrategy="BlockLineHeight" 
           Canvas.Left="2.724" LineHeight="71.312" 
           TextAlignment="Center" TextWrapping="Wrap" 
           Canvas.Top="69.985" Width="231.581" Text="09:00" />

And then I want to change the value of that TextBlock in WindowAfterLogin.xaml.cs, so I've done this :

MainWindow objMainWindow = new MainWindow();
WindowAfterLogin objAfterLogin = new WindowAfterLogin();                
objMainWindow.Show();
objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
this.Close();

But, when I press F5 button (Compile), it didn't change. Where is the problem here?

Upvotes: 0

Views: 132

Answers (2)

Teaman
Teaman

Reputation: 187

As a simplification to bit answer, try to write something like this instead of your code

MainWindow objMainWindow = new MainWindow();
objMainWindow.Show();
In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
this.Close(); //Not sure if You need it. Try to comment this line to be able to notice textblock text changes

Also I think You need

DateTime.Now.ToString("hh:mm") 

instead of

DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString()

Upvotes: 0

bit
bit

Reputation: 4487

Are you creating a separate instance of WindowAfterLogin ? If you want to do that, you will have to Show() the new instance. Try this:

    WindowAfterLogin objAfterLogin = new WindowAfterLogin();                
    objAfterLoginShow();
    objAfterLogin.In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
    //Close(); //Close any other window that needs to be closed..

If you wanted the In_Time.Text to change for the current instance, (assuming that has been showed or is visible currrently), you can try this perhaps in the constructor or your method that Initializes the WindowAfterLogin :

In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();

Upvotes: 1

Related Questions