简体   繁体   English

从用户控件访问主窗口

[英]Access to main window from user control

I have Login User Control , I want that when the user would click some button inside the user control I need it to make visible some textBox . 我有登录User Control ,我希望当用户单击用户控件内的某个按钮时,我需要它使一些textBox可见。

In main window I have: 在主窗口中,我有:

<local:LoginUserCon />
<TextBox x:Name="myTextBox" Visibility="Collapsed"/>

I tried: (In Login User Control): 我试过:(在登录用户控件中):

void Login_Btn(object sender, RoutedEventArgs e)
{
   Application.Current.MainWindow.myTextBox.Visiblity = Visibility.Visible;
}

But it says: 但它说:

'Window' does not contain a definition for 'myTextBox' and no accessible extension method 'myTextBox'... “窗口”不包含“ myTextBox”的定义,也没有可访问的扩展方法“ myTextBox” ...

Application.Current.MainWindow returns a Window . Application.Current.MainWindow返回一个Window You need to cast it to whatever your window type is, like for example MainWindow : 您需要将其强制转换为您的任何窗口类型,例如MainWindow

void Login_Btn(object sender, RoutedEventArgs e)
{
    MainWindow mainWindow = Application.Current.MainWindow as MainWindow;
    if (mainWindow != null)
        mainWindow.myTextBox.Visiblity = Visibility.Visible;
}

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

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