[英]How to refresh a second window with a button click on the first
How could I refresh a secodn window with a button click on the first window. 如何在第一个窗口上单击按钮刷新secodn窗口。
I am new to WPF and searched for long time but the answers I found didn't work. 我是WPF的新手,已经搜索了很长时间,但是我发现的答案不起作用。 I am trying to write in a textbox on the main that it get posted on the second window.
我正在尝试在第二个窗口上发布的主文本框中编写文本。 For so far I have that but I can only work with it when I use win1.show().
到目前为止,我已经拥有了,但是我只能在使用win1.show()时使用它。 This gives a new pop-up screen with the typed text but not on the same page.
这将显示一个新的弹出屏幕,其中包含键入的文本,但不在同一页面上。 So how could I get this instead of a pop up to refresh.
所以我怎么能得到这个而不是弹出来刷新。
Main window: 主视窗:
Title="MainWindow" Height="1050" Width="1680" ResizeMode="CanResizeWithGrip" Topmost="False" WindowState="Normal" Icon="./Resources/escapehaarlem_logo.ico" >
<Grid Background="Gray">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1593*"/>
<ColumnDefinition Width="80*"/>
</Grid.ColumnDefinitions>
<Button Content="Verstuur" HorizontalAlignment="Left" Margin="1330,10,0,0" VerticalAlignment="Top" Width="334" Height="85" Grid.ColumnSpan="2" Click="Verstuur_Button_Click"/>
<Button Content="Verwijder" HorizontalAlignment="Left" Margin="1330,100,0,0" VerticalAlignment="Top" Width="334" Height="85" Grid.ColumnSpan="2" Click="Verwijder_Button_Click_1"/>
<TextBox Name="Textbox" HorizontalAlignment="Left" Height="1000" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="1315"/>
</Grid>
Second window: 第二个窗口:
Title="MainWindow" Height="1050" Width="1680" ResizeMode="NoResize" Topmost="True" WindowState="Maximized" Icon="./Resources/escapehaarlem_logo.ico" >
<Grid Background="Black">
<Label Name="lbl_Tekst" Content="" HorizontalAlignment="Left" VerticalAlignment="Top" Foreground="White" Height="1020" Width="1673"/>
</Grid>
Code main window: 代码主窗口:
public partial class MainWindow : Window
{
string hint = "";
string verwijder = "";
Window1 win1 = new Window1();
public MainWindow()
{
InitializeComponent();
win1.Show();
}
private void Verstuur_Button_Click(object sender, RoutedEventArgs e)
{
hint = Textbox.Text;
Window1 win1 = new Window1(hint);
win1.Show();
}
private void Verwijder_Button_Click_1(object sender, RoutedEventArgs e)
{
Textbox.Text = verwijder;
Window1 win1 = new Window1(verwijder);
win1.Show();
}
}
Code second window: 代码第二个窗口:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
public Window1(string hint) : this()
{
lbl_Tekst.Content = hint;
}
}
Hope you could help to get it to refresh instead of a new popupscreen. 希望您可以帮助使其刷新而不是新的弹出屏幕。
Create a method on Window1
class to update the label. 在
Window1
类上创建一个方法来更新标签。
Like: 喜欢:
public void UpdateLabel(string text)
{
lbl_Tekst.Content = text;
}
You would then call it from your MainWindow
like this: 然后,您可以从
MainWindow
像这样调用它:
win1.UpdateLabel("I am updated content");
You should then have the content of that Label updated. 然后,您应该更新该标签的内容。 However I would advise you read up on MvvM and
DataContexts
it would make it a lot easier for you to manipulate contents of UI elements without actually referencing these elements. 但是,我建议您阅读MvvM和
DataContexts
这将使您无需实际引用这些元素即可更轻松地操作UI元素的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.