简体   繁体   English

从子用户控件更改 WPF 窗口的 label 内容

[英]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'我有 wpf window 命名为“GetStarted”,网格是用户控制“Step1”的一部分

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

on step1 is button with this code在第 1 step1是带有此代码的按钮

 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.但是当我按下btnNext时,什么也没有发生。

Your current code is creating a new Window instance.您当前的代码正在创建一个新的 Window 实例。 If you want to get the Window containing the UC you can call Window.GetWindow and then cast to your specific Window type:如果您想获得包含 UC 的 Window ,您可以调用 Window.GetWindow ,然后转换为您的特定 Window 类型:

    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";
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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