[英]How to pass a parameter in wpf C#
I have a code in WindowAfterLogin.xaml
: 我在
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 : 然后我想在
WindowAfterLogin.xaml.cs
更改该TextBlock
的值,所以我做到了:
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. 但是,当我按F5按钮(编译)时,它没有改变。 Where is the problem here?
问题出在哪里?
Are you creating a separate instance of WindowAfterLogin
? 您是否要创建
WindowAfterLogin
的单独实例? If you want to do that, you will have to Show()
the new instance. 如果要这样做,则必须
Show()
新实例。 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
(假定已显示或当前可见),则可以在构造函数或初始化WindowAfterLogin
方法中尝试进行此WindowAfterLogin
:
In_Time.Text = DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString();
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.