c# WPF Mainwindow 按钮在 UserControl 代码中无法识别?
c# WPF Mainwindow button is not recognized in UserControl code?
在主窗口的 xaml 中创建了一个名为“btnDel”的按钮,默认情况下为“Visibility = false”。
在我的 UserControlRDV.xaml.cs 代码中,我想使用“MainWindow.btnDel.Visibility = true”使该按钮可见。
在这里我收到错误 CS0120“非静态字段、方法或 属性 'MainWindow.btnDel' 需要对象引用?
请帮忙!!
谢谢
您可以使用 Window.GetWindow
方法获取对 UserControl
的父 window 的引用:
MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
if (mainWindow != null)
mainWindow.btnDel.Visibility = Visibility.Visible;
在主窗口的 xaml 中创建了一个名为“btnDel”的按钮,默认情况下为“Visibility = false”。 在我的 UserControlRDV.xaml.cs 代码中,我想使用“MainWindow.btnDel.Visibility = true”使该按钮可见。 在这里我收到错误 CS0120“非静态字段、方法或 属性 'MainWindow.btnDel' 需要对象引用?
请帮忙!!
谢谢
您可以使用 Window.GetWindow
方法获取对 UserControl
的父 window 的引用:
MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
if (mainWindow != null)
mainWindow.btnDel.Visibility = Visibility.Visible;