显示 child 在 wpf 中使用用户控件
Showing child using user control in wpf
招聘:
需要在主布局中显示所有 child 页面。如果不作为新 window 打开,我的意思是它不应该作为与主 window
分开的 window 可见
我想到的解决方案:
我在主页中使用了内容展示器。
将所有其他页面创建为用户控件。
单击菜单 ViewWindow.Content = new SalesEntry();
通过使用它,我正在展示它。
问题:
要关闭该用户控件,我使用了单击按钮(用户控件中存在的按钮)
预成型 this.Visibility = Visibility.Hidden;
但每次用户请求此页面时,页面都会初始化并显示。
那么,克服这个问题的最佳方法是什么,或者解决这个问题的任何其他方法是什么。
(我被告知不要使用任何框架作为项目需求)
我是 WPF 新手..
请帮帮我..
你做的很好,我不太明白这里的问题,但我会告诉你我会怎么做。
您将有一个 parent 视图,一个 Window。您将有许多 child 用户控件。
在您的 window 中,您应该有一种 select 显示哪个 child 的方法。这可以使用按钮或菜单来完成。
当你 select 一个 child 时,你将它实例化为一个 object 并订阅它的退出事件。当 child 触发此事件时,您将 child 从 parent window.
中的 child 中删除
// 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;
}
}
}
如果您遇到其他问题,请发表评论。
招聘:
需要在主布局中显示所有 child 页面。如果不作为新 window 打开,我的意思是它不应该作为与主 window
分开的 window 可见我想到的解决方案:
我在主页中使用了内容展示器。
将所有其他页面创建为用户控件。
单击菜单 ViewWindow.Content = new SalesEntry();
通过使用它,我正在展示它。
问题:
要关闭该用户控件,我使用了单击按钮(用户控件中存在的按钮)
预成型 this.Visibility = Visibility.Hidden;
但每次用户请求此页面时,页面都会初始化并显示。
那么,克服这个问题的最佳方法是什么,或者解决这个问题的任何其他方法是什么。 (我被告知不要使用任何框架作为项目需求)
我是 WPF 新手..
请帮帮我..
你做的很好,我不太明白这里的问题,但我会告诉你我会怎么做。
您将有一个 parent 视图,一个 Window。您将有许多 child 用户控件。
在您的 window 中,您应该有一种 select 显示哪个 child 的方法。这可以使用按钮或菜单来完成。
当你 select 一个 child 时,你将它实例化为一个 object 并订阅它的退出事件。当 child 触发此事件时,您将 child 从 parent window.
中的 child 中删除// 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;
}
}
}
如果您遇到其他问题,请发表评论。