Reputation: 7361
I have a few WPF windows that my projects go between by calling windowA.Close() and windowB.Show(). I need the change to occur without any transition (ie. the animation of one window closing and another opening). Of course I could make one really messy window with the functionality of multiple ones, but is there a simpler way to do this?
Upvotes: 0
Views: 190
Reputation: 672
MVVM pattern is a nice way to do this. You can make a single outer Window as a shell with a that swaps in and out UserControls that represent the functionality of windowA and windowB. Implementing MVVM is a bit much to cover in an answer; here is a good idea of a basic implementation of it that would be more-than-sufficient for what you're trying to do.
This assumes:
1) You're implementing MVVM, and
2) You have a view model and Window for your main "shell" window, and a view model and UserControl for your "sub-windows."
Anyway, in your view model for the main Window, you need a property for the view model of your child view, like this:
private object currentView;
public object CurrentView
{
get{ return this.currentView;}
set
{
this.currentView = value;
RaisePropertyChanged("CurrentView"); //assumes you've implemented an MVVM pattern with a method to notify the view when a property has changed like the link above
}
Then in your XAML, you add data templates for each child view in :
This is saying, "if something in this view has a property bound to it with a value of WindowAViewModel, then show WindowAView in its place."
Then you have your ContentControl bound to the CurrentView property:
<ContentControl Content="{Binding CurrentView}"/>
Finally, in your main window view model code, when you want to switch to the A or B view, you simply do:
this.CurrentView = new WindowAViewModel(); //along with whatever initialization needs to occur
Since the property is set to notify when it is changed, it notifies the view that there's something new in CurrentView, and the DataTemplates tell the main window what to render for that something new.
Upvotes: 1