How to open and close popup window from parent in WPF using MVVM?
Please see my below code,tell me the solution for issue.
I create separate model,view,viewmodel for child usercontrol and parent usercontrol.
I try to open the popup window from parent window.I create separate view,viewmodel for child usercontrol and used inside the popup control of parent usercontrol.
I am getting issue and also how to close the popup window from the button click of child usercontrol.
I am getting this below issue:
-----------------------------
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'No matching constructor found on type 'parentchild.View. ParentView.'. You can use the Arguments or FactoryMethod directives to construct this type.' Line number '9' and line position '10'.
If there is a handler for this exception, the program may be safely continued.
Here I am using WPF using MVVM pattern with Prism Framework.
MainWindow.xaml:
----------------
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfLab.Controls;assembly=WpfLab.Controls"
xmlns:local="clr-namespace:parentchild.View"
Title="MainWindow" Height="768" Width="1280">
parentView.xaml:
------------
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:controls="clr-namespace:parentchild.View"
xmlns:vm="clr-namespace:parentchild.ViewModel"
mc:Ignorable="d"
d:DesignHeight="768" d:DesignWidth="1280">
parentView.xaml.cs:
----------------
public partial class parentView : UserControl
{
public parentView(ParentViewModel parentViewModel)
{
InitializeComponent();
this.DataContext = parentViewModel;
}
}
ParentViewModel.cs:
----------------
public class ParentViewModel : INotifyPropertyChanged
{
private readonly IEventAggregator eventAggregator;
private RumModel currenrumModel;
private string resultMessage;
private bool _popupIsOpen;
public RumViewModel(IEventAggregator eventAggregator)
{
currenrumModel = new RumModel();
this.tosscardCommand = new DelegateCommand
if (eventAggregator == null) throw new ArgumentNullException("eventAggregator");
this.eventAggregator = eventAggregator;
}
public bool IsPopupIsOpen
{
get { return _popupIsOpen; }
set
{
_popupIsOpen = value;
NotifyPropertyChanged("IsPopupIsOpen");
}
}
public RumModel currentRum
{
get
{
return currenrumModel;
}
set
{
currenrumModel = value;
}
}
public bool tossButtonEnabled
{
get { return this.currenrumModel.tossButtonIsEnabled; }
set
{
this.currenrumModel.tossButtonIsEnabled = value;
this.NotifyPropertyChanged("tossButtonEnabled");
}
}
public Visibility popupVisibility
{
get { return this.currenrumModel.popupVisibility; }
set
{
this.currenrumModel.popupVisibility = value;
this.NotifyPropertyChanged("popupVisibility");
}
}
#region commands
public ICommand tosscardCommand { get; private set; }
public ICommand cancelGameCommand { get; private set; }
#endregion
private bool CanExecute(object arg)
{
return true;
}
private void Execute(object arg)
{
popupVisibility = Visibility.Visible;
IsPopupIsOpen = true;
eventAggregator.GetEvent().Subscribe(PopupAction, ThreadOption.UIThread);
}
private void PopupAction(bool popup)
{
IsPopupIsOpen = popup;
}
private void RaiseCanExecuteChanged()
{
DelegateCommand
tosscommand.RaiseCanExecuteChanged();
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
ChildWindowView.xaml:
--------------------
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:viewModels="clr-namespace:parentchild.ViewModel"
xmlns:views="clr-namespace:parentchild.View"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="500">
ChildWindowView.xaml.cs:
--------------------
public partial class ChildWindowView : UserControl
{
public ChildWindowView(ChildWindowView child)
{
InitializeComponent();
this.DataContext = child;
}
}
ChildWindowViewModel.cs:
------------------------
public class ChildWindowViewModel : INotifyPropertyChanged, IInteractionRequestAware
{
private ChildWindowModel currentChildWindow;
private readonly IEventAggregator eventAggregator;
private IService service;
public ChildWindowViewModel(IEventAggregator eventAggregator,IService service)
{
currentChildWindow = new ChildWindowModel();
this.eventAggregator = eventAggregator;
this.service = service;
this.SubmitCommand = new DelegateCommand(this.OnSubmit, this.CanSubmit);
}
private void RaiseCanExecuteChanged()
{
DelegateCommand command = SubmitCommand as DelegateCommand;
command.RaiseCanExecuteChanged();
}
public ChildWindowModel currentTimer
{
get
{
return currentTimer;
}
set
{
currentTimer = value;
}
}
public string setTimer
{
get { return this.currentChildWindow.setTimer; }
set
{
if (this.currentChildWindow.setTimer != value)
{
this.currentChildWindow.setTimer = value;
NotifyPropertyChanged("setTimer");
RaiseCanExecuteChanged();
}
}
}
public string errMessage
{
get { return this.currentChildWindow.errMessage; }
set
{
if (this.currentChildWindow.errMessage != value)
{
this.currentChildWindow.errMessage = value;
NotifyPropertyChanged("errMessage");
RaiseCanExecuteChanged();
}
}
}
public ICommand SubmitCommand { get; private set; }
private bool CanSubmit(object arg)
{
return true;
}
private void OnSubmit(object arg)
{
try
{
if (string.IsNullOrEmpty(setTimer))
{
this.errMessage = "Please enter Rescheduler Timer value";
}
if (this.FinishInteraction != null)
this.FinishInteraction();
}
catch (Exception ex)
{
throw ex;
}
}
public Action FinishInteraction { get; set; }
public INotification Notification { get; set; }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
Replies
Know the answer? Post it — somebody with the same question will find it here.