简体   繁体   English

WPF从子窗口更新父控件状态

[英]WPF Update Parent Control Status from child Window

I have got a status bar in Main Windows and I use User control as a child inside the main window. 我在主窗口中有一个状态栏,并且在主窗口中以用户控件作为子控件使用。 I just want to update TextBox in the status bar which is inside the Main windows. 我只想更新主窗口内状态栏中的TextBox。

 <Grid x:Name="grControls">             

 </Grid>    
 <StatusBar>
    <StatusBarItem Grid.Column="5">
        <TextBlock Name="NumberOfItemTextBox" >No:</TextBlock>
    </StatusBarItem>
 </StatusBar>

// CODE BEHIND I load Child windows into "grContorls"
        grControls.Children.Clear();
        var userCont = new PAMHeaderUC
        {
            ParentControl = this
        };
        grControls.Children.Add(userCont);

I just want to update NumberOfItemTextBox in child Usercontrol. 我只想更新子用户控件中的NumberOfItemTextBox。

These is code behind solution. 这些是解决方案背后的代码。 If you are using MVVM it will be much cleaner. 如果您使用的是MVVM,它将更加干净。 From your usercontrol code behind, you can get your window, then you cast your window into your main window class that derives from Window and you got access to textBox 从后面的用户控件代码中,您可以获取窗口,然后将窗口转换为从Window派生的主窗口类,并可以访问textBox

Window parentWindow = Window.GetWindow(userControlRefernce);
var yourInstanceOfWindow = (CastToYourNameOfClass)parentWindow;
yourInstanceOfWindow.NumberOfItemTextBox.Text = "someString";

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

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