Minimize Child Window in WPF When Owner Window Of

2019-07-27 22:36发布

I have a Window in WPF which is the Owner of a Window, Which is the Owner of all the Other Windows.

The main Owner (sideWindow) is just 200points in Width is placed in the left and the other Window is placed right of the main Window. sideWindow has button which opens form as the child window of the right window.

When I minimize the mainWindow (sideWindow) only the right window is minimized and the rest child window are not minimized.

How can I achieve this?

标签: c# .net wpf mvvm
3条回答
▲ chillily
2楼-- · 2019-07-27 22:53

I'll have to do a little bit guessing here, as I don't really know the architecture of your program. I'm going to assume you have two Windows classes: MainWindow and SideWindow, and a single view model class AppVm. You'll have to adapt if you have a different settings.

The simplest solution would be to bound the WindowState of both windows to a single property in the ViewModel. Make sure the property fires notifications.

The View Model class:

class AppVm : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    private WindowState m_state;
    public WindowState state {
        get { return m_state; }
        set { m_state=value; raise("state") }
    }
    private raise(string propname) {
        PropertyChanged(this, new PropertyChangedEventArgs(propname));
    }
    ....
}

Now, in the Windows XAML files, bind the WindowState to the one on the view model:

<Window ... WindowState="{Binding Path=state,Mode=TwoWay} ...>

Note how I set the mode to TwoWay. This ensure that when the state is changed when the using clicking the minimize Window, it will also change the value in the VM. That's all.

Now, if your architecture differs, you'll have to adapt. It isn't clear what you want to do when the user maximize one of the windows (do you even allow it). If you have two different view model classes for each Window, you can write down a more complex path, assuming one view has a reference to the other view, save the stae variable only in the inner class, then the binding statement can be: Path=OtherVm.state.

If your View logic is more complex, It is OK to have some code behind in the view (*.xaml.cs files). The only rule is that the View Model shouldn't be "aware" of the view (theoritically, the VM can also run a "unit test view"). You can subscribe to the event StateChanged of the View and write down your logic there. Anyway, the cleanest solution is described in the sample code above.

Hope this helped.

查看更多
3楼-- · 2019-07-27 23:00

The easiest way to do this is to the Owner property of each child window to point to the main window. Are you doing this?

查看更多
forever°为你锁心
4楼-- · 2019-07-27 23:11

You should use a Messaging solution to achive what you want. GalaSofts MVVM light has a built in Messenger. If you minimize your Window i guess you do it with a command and then in the ViewModel you can send a Message and with the others subscribe to this Message and minimize the Windows. I guess there are other Messenger solutions too just try to Google it.

GalaSoft MVVM Light

查看更多
登录 后发表回答