简体   繁体   中英

Change WPF window's label content from child user control

I have wpf window named 'GetStarted' with grid which is perent of user control 'Step1'

Step1 s1 = new Step1();
mainGrid.Children.Add(s1);

on step1 is button with this code

 private void btnNext_Click(object sender, RoutedEventArgs e)
 {
            etStarted gt = new GetStarted();
            gt.image0.Visibility = Visibility.Visible;
            gt.lblSteps.Content= "Step 2 of 5";
 }

but when I press btnNext nothing happens.

Your current code is creating a new Window instance. If you want to get the Window containing the UC you can call Window.GetWindow and then cast to your specific Window type:

    private void btnNext_Click(object sender, RoutedEventArgs e)
    {
        var gt = Window.GetWindow(this) as GetStarted;
        if (gt != null)
        {
            gt.image0.Visibility = Visibility.Visible;
            gt.lblSteps.Content = "Step 2 of 5";
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM