简体   繁体   English

如何使用按钮刷新第二个窗口

[英]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.

相关问题 我可以通过单击第一个孩子的按钮在同一个 MDI window 中显示第二个孩子吗? - Can I through click the button of first child to show the second child in same MDI window? 点击第一个按钮启动计时器,第二个按钮停止 - First button click to start timer, second to stop 如何首先单击更改按钮图像,然后单击显示预览图像(按钮的第一个图像)? - how do i can first click changing button image and second click show preview image (first image for button)? 单击弹出窗口中的按钮后,如何刷新上一页? - How to make previous page refresh after I click a button in pop-up window? 如何在第一个用户控件中通过单击按钮在面板中添加第二个用户控件 - How to add second user control in panel by button click in first user control 在 C# 中,当我单击第二个表单的按钮时,如何在第一个表单中打开第三个表单 - In C# how to open third form inside first form when i click a button of the second form WPF:可以在单击时打开和关闭第二个窗口的按钮吗? (打开) - WPF: A button to both open and close a second window on click? (toggle open) 如何在单击按钮时刷新Web用户控件 - How to refresh Web Usercontrol on button click 如何在WPF中单击按钮单击时刷新用户控件 - How to refresh user control on the button click in WPF 如何刷新页面? (单击按钮关闭RadWindow) - How to refresh the page? (Click button close RadWindow)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM