gpt4 book ai didi

wpf - 从主窗口网格 WPF 中的用户控件打开用户控件

转载 作者:行者123 更新时间:2023-12-04 20:14:03 25 4
gpt4 key购买 nike

也许首先我会谈谈我的申请。
例如,当员工登录我的应用程序时,他将加载“员工菜单”:

    Dim Empl As New Employee
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)

这是来自 Window_Loaded 事件。菜单是用户控件,我有几个按钮可以打开和操作另一个用户控件。例如,当我按下“问题”按钮时:
Public Class Employee
Dim mw As New MainWindow
Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
Dim Que As New QuestionAdd
mw.MainGrid.Children.Add(Que)
Grid.SetRow(Que, 2)
Grid.SetColumn(Que, 1)
End Sub
End Class

我不知道为什么 button_click 后没有加载任何内容.....
从其他控件导航主窗口网格有那么难吗?

最佳答案

这只是一个猜测,因为您没有提供太多信息,但我注意到以下问题可能是罪魁祸首。

在您的第一个代码片段中,您似乎是从 MainForm 创建员工:

Dim Empl As New Employee
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)

您的以下评论似乎证实了这一假设:

This is from Window_Loaded event. Menu is User Control, and there I have few buttons to open and operate another user controls. When I press for example button "Question"



然而,在您的 Employee 类中,您正在创建一个全新的 MainWindow 实例,然后向其中添加数据:
Public Class Employee
Dim mw As New MainWindow
Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
Dim Que As New QuestionAdd
mw.MainGrid.Children.Add(Que)
Grid.SetRow(Que, 2)
Grid.SetColumn(Que, 1)
End Sub
End Class

如果这个观察是正确的,那么我认为你需要回到书本并理解 classes的概念。和 instances .

您基本上创建了第二个表单(它被隐藏,因为您从未明确显示它),然后修改第二个表单而不是原始表单。为了证明这个假设,尝试添加以下代码行:
Public Class Employee
Dim mw As New MainWindow
Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
Dim Que As New QuestionAdd
mw.MainGrid.Children.Add(Que)
Grid.SetRow(Que, 2)
Grid.SetColumn(Que, 1)
mw.Show() ' <---
End Sub
End Class

您可能会看到第二个表单弹出,其中包含您对第一个表单所期望的所有更改。

至于如何解决这个问题,最简单的方法是向您的初始化程序(“Sub New”)添加一个参数,该参数接受 MainForm 作为值。然后,您可以将值分配给一个字段或属性(可能只是您的 mw 字段)并继续您的快乐方式。但是,这会让您头疼,所以现在可能是开始了解更多有关 software architecture 的好时机。 ,尤其是 separation of concerns的概念.

关于wpf - 从主窗口网格 WPF 中的用户控件打开用户控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18210688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com