Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I feel I just found a rather nice MVVM solution to this problem. I wrote a behavior that is exposing a type property <code>WindowType</code> and a boolean property <code>Open</code>. DataBinding the latter allows the ViewModel to open and close the windows easily, without knowing anything about the View.</p> <p>Gotta love behaviors... :)</p> <p><img src="https://i.stack.imgur.com/b5VVi.png" alt="enter image description here"></p> <p>Xaml:</p> <pre><code>&lt;UserControl x:Class="WpfApplication1.OpenCloseWindowDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WpfApplication1" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"&gt; &lt;UserControl.DataContext&gt; &lt;local:ViewModel /&gt; &lt;/UserControl.DataContext&gt; &lt;i:Interaction.Behaviors&gt; &lt;!-- TwoWay binding is necessary, otherwise after user closed a window directly, it cannot be opened again --&gt; &lt;local:OpenCloseWindowBehavior WindowType="local:BlackWindow" Open="{Binding BlackOpen, Mode=TwoWay}" /&gt; &lt;local:OpenCloseWindowBehavior WindowType="local:YellowWindow" Open="{Binding YellowOpen, Mode=TwoWay}" /&gt; &lt;local:OpenCloseWindowBehavior WindowType="local:PurpleWindow" Open="{Binding PurpleOpen, Mode=TwoWay}" /&gt; &lt;/i:Interaction.Behaviors&gt; &lt;UserControl.Resources&gt; &lt;Thickness x:Key="StdMargin"&gt;5&lt;/Thickness&gt; &lt;Style TargetType="Button" &gt; &lt;Setter Property="MinWidth" Value="60" /&gt; &lt;Setter Property="Margin" Value="{StaticResource StdMargin}" /&gt; &lt;/Style&gt; &lt;Style TargetType="Border" &gt; &lt;Setter Property="Margin" Value="{StaticResource StdMargin}" /&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; &lt;Grid&gt; &lt;StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Border Background="Black" Width="30" /&gt; &lt;Button Content="Open" Command="{Binding OpenBlackCommand}" CommandParameter="True" /&gt; &lt;Button Content="Close" Command="{Binding OpenBlackCommand}" CommandParameter="False" /&gt; &lt;/StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Border Background="Yellow" Width="30" /&gt; &lt;Button Content="Open" Command="{Binding OpenYellowCommand}" CommandParameter="True" /&gt; &lt;Button Content="Close" Command="{Binding OpenYellowCommand}" CommandParameter="False" /&gt; &lt;/StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Border Background="Purple" Width="30" /&gt; &lt;Button Content="Open" Command="{Binding OpenPurpleCommand}" CommandParameter="True" /&gt; &lt;Button Content="Close" Command="{Binding OpenPurpleCommand}" CommandParameter="False" /&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>YellowWindow (Black/Purple alike):</p> <pre><code>&lt;Window x:Class="WpfApplication1.YellowWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="YellowWindow" Height="300" Width="300"&gt; &lt;Grid Background="Yellow" /&gt; &lt;/Window&gt; </code></pre> <p>ViewModel, ActionCommand:</p> <pre><code>using System; using System.ComponentModel; using System.Windows.Input; namespace WpfApplication1 { public class ViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (this.PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } private bool _blackOpen; public bool BlackOpen { get { return _blackOpen; } set { _blackOpen = value; OnPropertyChanged("BlackOpen"); } } private bool _yellowOpen; public bool YellowOpen { get { return _yellowOpen; } set { _yellowOpen = value; OnPropertyChanged("YellowOpen"); } } private bool _purpleOpen; public bool PurpleOpen { get { return _purpleOpen; } set { _purpleOpen = value; OnPropertyChanged("PurpleOpen"); } } public ICommand OpenBlackCommand { get; private set; } public ICommand OpenYellowCommand { get; private set; } public ICommand OpenPurpleCommand { get; private set; } public ViewModel() { this.OpenBlackCommand = new ActionCommand&lt;bool&gt;(OpenBlack); this.OpenYellowCommand = new ActionCommand&lt;bool&gt;(OpenYellow); this.OpenPurpleCommand = new ActionCommand&lt;bool&gt;(OpenPurple); } private void OpenBlack(bool open) { this.BlackOpen = open; } private void OpenYellow(bool open) { this.YellowOpen = open; } private void OpenPurple(bool open) { this.PurpleOpen = open; } } public class ActionCommand&lt;T&gt; : ICommand { public event EventHandler CanExecuteChanged; private Action&lt;T&gt; _action; public ActionCommand(Action&lt;T&gt; action) { _action = action; } public bool CanExecute(object parameter) { return true; } public void Execute(object parameter) { if (_action != null) { var castParameter = (T)Convert.ChangeType(parameter, typeof(T)); _action(castParameter); } } } } </code></pre> <p>OpenCloseWindowBehavior:</p> <pre><code>using System; using System.Windows; using System.Windows.Controls; using System.Windows.Interactivity; namespace WpfApplication1 { public class OpenCloseWindowBehavior : Behavior&lt;UserControl&gt; { private Window _windowInstance; public Type WindowType { get { return (Type)GetValue(WindowTypeProperty); } set { SetValue(WindowTypeProperty, value); } } public static readonly DependencyProperty WindowTypeProperty = DependencyProperty.Register("WindowType", typeof(Type), typeof(OpenCloseWindowBehavior), new PropertyMetadata(null)); public bool Open { get { return (bool)GetValue(OpenProperty); } set { SetValue(OpenProperty, value); } } public static readonly DependencyProperty OpenProperty = DependencyProperty.Register("Open", typeof(bool), typeof(OpenCloseWindowBehavior), new PropertyMetadata(false, OnOpenChanged)); /// &lt;summary&gt; /// Opens or closes a window of type 'WindowType'. /// &lt;/summary&gt; private static void OnOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var me = (OpenCloseWindowBehavior)d; if ((bool)e.NewValue) { object instance = Activator.CreateInstance(me.WindowType); if (instance is Window) { Window window = (Window)instance; window.Closing += (s, ev) =&gt; { if (me.Open) // window closed directly by user { me._windowInstance = null; // prevents repeated Close call me.Open = false; // set to false, so next time Open is set to true, OnOpenChanged is triggered again } }; window.Show(); me._windowInstance = window; } else { // could check this already in PropertyChangedCallback of WindowType - but doesn't matter until someone actually tries to open it. throw new ArgumentException(string.Format("Type '{0}' does not derive from System.Windows.Window.", me.WindowType)); } } else { if (me._windowInstance != null) me._windowInstance.Close(); // closed by viewmodel } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload