Open and close child window from other

Feb 11, 2014 at 5:42 PM
Edited Feb 11, 2014 at 5:44 PM
There are any another way to show child window except?
WindowContainer.Children.Add(new MdiChild()
{
    Title = "Window 1"
});
I have a mvvm based project and I try to show child window on click from another window, but I couldn't find any "Show" method or etc. I try to working with "Visibility" property, but, there is no any sense in this property, because, when I click "close" button, "Visibility" is still have value "visible", but, window is hidden.
My "view of model" looks

    class DataEntryViewModel
    {
        public ChildWindow First { get; set; }
        public DataEntryViewModel()
        {
            ClickCommand = new Click(arg => ClickMethod());
            First = new ChildWindow
            {
                Visual = "Hidden",
                Title = "Project Grid"
            };
        }

        public ICommand ClickCommand { get; set; }
        private void ClickMethod()
        {
            First.Visual = "Visible";
        }
    }
How to resolve this issue?
Feb 11, 2014 at 6:59 PM
Edited Feb 11, 2014 at 7:01 PM
Hello! Use 'ref'.

See if it helps you:

OPEN THE FATHER SON OF ANOTHER CHILD:

Change in children:
MdiContainer refContainer MdiContainer = new (); 

public Child (ref MdiContainer container) {
             InitializeComponent (); 
             refContainer = container; 
         } 
Opening of the father:
Son instance= new Son(ref Container);
Container.Children.Add(new MdiChild { Content = instance, Title = "Title Son" , Width = 500, Height = 500 });
Open a son of another child pro father:
Son2 son2= new Son2(refContainer);
                refContainer.Children.Add(new MdiChild { Content = son2, Title = "Son2 ", Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, Height = 500 });
CLOSE OTHER SON OF A SON IN FATHER:

To close, you can use other different ways, in my case the title was the most appropriate:
foreach(MdiChild child in refContainer.Children.ToList())
            {
                if(((dynamic)child).Title.ToString().Contains("Son2"))
                    refContainer.Children.Remove(child);
            }
We started and finished our second son from the first child. I believe that this helps you!
NOTE: Sorry my bad English!
Feb 11, 2014 at 8:59 PM
Edited Feb 11, 2014 at 9:05 PM
Thanks, good solution I guess.
But, I'd already resolved it by myself.
In window "A" I set "DataContext" property from window "B"
            DataContext = MDIapp.View.B.dataContext;
In window "B" its looks like
        public static object dataContext;
        public B()
        {
            InitializeComponent();
            DataContext = new DataEntryViewModel(Container);
            dataContext = DataContext;
        }
There is bindind for property "Command" in xaml of Window "A"
        <Button Content="{Binding ProjectGrid.Title}" Height="23" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="157" Command="{Binding AddProjectGrid}" />
My "ViewModel.cs" looks:
class ViewModel
    {
        public ChildWindow ProjectGrid { get; set; }
        public ICommand AddProjectGrid { get; set; }
        public WPF.MDI.MdiContainer Container;

        public ViewModel(WPF.MDI.MdiContainer container)
        {
            Container = container;
            ProjectGrid = new ChildWindow { Title = "Title" };
            AddProjectGrid = new Command(arg => AddChild(ProjectGrid));
        }
        private void AddChild(ChildWindow child)
        {
            if (Container.Children.ToList<MdiChild>().Find(x => x.Title == child.Title) != null ? Container.Children.ToList<MdiChild>().Find(x => x.Title == child.Title).Title != child.Title : true)
            {
                Container.Children.Add(new MdiChild()
                {
                    Title = child.Title,
                    Content = new DataGrid()
                });
            }
        }
So, it works perfect for me and I'm very glad!