How to add my predesigned WPF form as MdiChild

Mar 18, 2010 at 10:25 AM

Hello,

is there a way to add predesigned form to MdiParent through code? For example

partial class MyWpfForm : Window
{
       // some code here
}

....

//asume mdiContainer is declared in Main.xaml somewhere as shown in Your example

private void button1_Click(object sender, RoutedEventArgs e)
{
       MyWpfForm myForm = new MyWpfForm();
       mdiContainer.Children.Add(myForm);
}

Thank You in advance

Jun 21, 2010 at 10:07 PM

It can't even do that? FAIL.

Oct 31, 2010 at 6:11 PM
Edited Oct 31, 2010 at 6:23 PM

it's not hard. There is a sample for a predesigned login window:

 

            LoginWindow window=new LoginWindow();
            WindowContainer.Children.Add(new MdiChild()
            {
                Title = window.Title,
                Content =window.Content as UIElement
                Width = window.Width,
                Height = window.Height
            });

 

Feb 8, 2011 at 5:39 AM

Hello

I want to add a new user-control on other user-control button click and want to close previous one


Main objMainWin = new Main();
 objMainWin.Container.Children.Add(new MdiChild
  {               
                Title = "Select MYOB Company File- Corecon Link Application",
                Width = 315,
                Height = 100,
                Icon = new BitmapImage(new Uri("../favicon.ico", UriKind.Relative)),
                Position = new Point(200, 80),
                Content = new UserControl1()
});

I am doing like this but it is not working?

Please suggest ?

Feb 8, 2011 at 6:56 AM
Edited Feb 8, 2011 at 7:03 AM

Hello,

To close previous control (MdiChild), you can either:

1) Store link to it in variable:

MdiChild mdiChild = new MdiChild { ... };
objMainWin.Container.Children.Add(mdiChild);
...
objMainWin.Container.Children.Remove(mdiChild);

2) Remove top child:

objMainWin.Container.Children.Remove(objMainWin.Container.ActiveMdiChild);

3) Call Close method of MdiChild control:

mdiChild.Close();
objMainWin.Container.ActiveMdiChild.Close();

Hope this helps.

Feb 8, 2011 at 8:43 AM
Edited Feb 8, 2011 at 11:36 AM

Hello 

In main form on Constractor  I am adding an Usercontrol like this

Container.Children.Add(new MdiChild
            {
                Title = "Window Using Code",
                Content = new ExampleControl(),
                Width = 200,
                Height = 200,
                Position = new Point(300, 80)
            });

and on that usercontrol (ExampleControl) i have a button and on that button click adding another usercontrol (ExampleControl1) to main form and removing that like this

Main objMainWin = new Main();           
            MdiChild mdiChild = new MdiChild           
            {               
                Title = "Select MYOB Company File- Corecon Link Application",
                Width = 300,
                Height = 100,
                Position = new Point(300, 80),
                Icon = new BitmapImage(new Uri("../favicon.ico", UriKind.Relative)),               
                Content = new UserControl1()
            };

            objMainWin.Container.Children.Add(mdiChild);

Plz suggest what have to do?

Feb 8, 2011 at 9:54 AM
Edited Feb 8, 2011 at 9:55 AM

Well, WPF.MDI is not an issue here.
It's your own choice to use whatever you want ;)

From little info you provided, it's easier to make event in ExampleControl and handler in Main.
1) In ExampleControl somewere:

public event Action<UserControl> ButtonPressed;

2) In ExampleControl's button handler:

if (ButtonPressed != null) ButtonPressed(this);

3) In Main constructor (when you're creating MdiChild):

examplecontrol.ButtonPressed += ButtonPressed_Handler;

4) In Main somewhere:

void ButtonPressed_Handler(UserControl content)
{
    MdiChild child = Container.Children.FirstOrDefault(t => t.Content == content);
    if (child != null)
        Container.Children.Remove(child);
}

Hope this helps.

 

Feb 8, 2011 at 11:53 AM

System.Collections.ObjectModel.ObservableCollection<WPF.MDI.MdiChild>' does not contain a definition for 'FirstOrDefault'

and also for MdiChildTitleChanged/ ActiveMdiChild I am getting this error

Feb 8, 2011 at 1:37 PM

using System.Linq;

Feb 11, 2011 at 12:06 PM
Edited Feb 11, 2011 at 12:07 PM

Hi

Thx :)

, Now i am opening a window in popup window from MDIChild on button click, it is working fine only issue is its comes behind of mainwindow, so it is not visible , how can i make it visible on main window?

Winow1 objWindow = new Winow1();
                            objWindow.Closed += new EventHandler(delegate(object sender, EventArgs e)
                            {
                              
                            });
                            objWindow.ShowDialog();

 

or also tried with objWindow.Owner.ShowDialog();

Feb 14, 2011 at 8:10 AM

HI

Now i am opening a window in popup window from MDIChild on button click, it is working fine only issue is its comes behind of mainwindow, so it is not visible , how can i make it visible on main window?

Winow1 objWindow = new Winow1();
                            objWindow.Closed += new EventHandler(delegate(object sender, EventArgs e)
                            {
                              
                            });
                            objWindow.ShowDialog();

 

or also tried with objWindow.Owner.ShowDialog();

Feb 17, 2011 at 12:42 PM

Either can't reproduce or can't get what you mean.
Wrote following code:

StackPanel sp = new StackPanel { Orientation = System.Windows.Controls.Orientation.Vertical };
Container.Children.Add(
	new MdiChild()
	{
		Title = "Button test",
		Content = sp,
		Width = 300,
		Height = 300
	});
Button bt = new Button { Content = "Message" };
sp.Children.Add(bt);
bt.Click += (s, e) => MessageBox.Show("Test");
bt = new Button { Content = "MdiChild" };
sp.Children.Add(bt);
bt.Click += (s, e) =>
{
	MdiChild child = new MdiChild { Title = "Test!", Content = new Label { Content = "Hey, can u see me?" } };
	Container.Children.Add(child);
};
Both MessageBox and MdiChild shows on top of parent.

Feb 25, 2011 at 8:13 AM

Hi

Can we reload one child from other child close

like I have one MDIChild and opened a new other MDIChild now i want to reload first MDiChild on other close

How is it possible?

Feb 25, 2011 at 12:13 PM

It looks like general question, not related to WPF.MDI.

MdiChild has public events, Closing and Closed.
The rest is up to you.

Mar 3, 2011 at 6:18 AM

Hi MDI container is not allowing to refresh grid data when one child is closed.( I want to refresh one MDI child from other close )

Data is updated but not reflected in grid.