[英]Showing child using user control in wpf
招聘人數:
需要以主版面顯示所有子頁面。 如果不作為新窗口打開,我的意思是不應將其作為與主窗口分開的單獨窗口顯示
我想出的解決方案:
我在主頁上使用了內容演示器。 創建所有其他頁面作為用戶控件。 在菜單上單擊ViewWindow.Content = new SalesEntry();
通過使用我顯示。
問題:
要關閉該用戶控件,我使用了按鈕單擊(用戶控件內部存在的按鈕)來執行此操作this.Visibility = Visibility.Hidden;
但是每次用戶請求此頁面時,頁面都會初始化並顯示。
那么,什么是克服或克服此問題的最佳方法呢? (被告知不要使用任何框架作為項目要求)
我是WPF的新手。
請幫助我。
您在做什么很好,我在這里不太了解問題,但是我會告訴您我將如何做。
您將有一個父視圖,一個窗口。 您將有許多孩子,UserControl。
在窗口內,您應該有一種選擇顯示哪個孩子的方法。 可以使用按鈕或菜單來完成。
選擇子項時,可以將其實例化為對象並訂閱其退出事件。 當孩子觸發此事件時,您可以在父窗口中從孩子中刪除該孩子。
// This one defines the signature of your exit event handler
public delegate void OnExitHandler(UserControl sender);
// This is your child, UserControl
public partial class MyChild : UserControl
{
public event OnExitHandler OnExit;
public MyChild()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.OnExit(this);
}
}
// This is your parent, Window
public partial class MainWindow : Window
{
private MyChild _control; // You can have a List<UserControl> for multiple
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
_control = new MyChild();
_control.OnExit += _control_OnExit; // Subscribe to event so you can remove the child when it exits
_content.Content = _control; // _content is a ContentControl defined in Window.xaml
}
private void _control_OnExit(UserControl sender)
{
if(sender == _control)
{
// Or if you have a collection remove the sender like
// _controls.Remove(sender);
_control = null;
_content.Content = null;
}
}
}
如果您還有其他問題,請發表評論。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.