Beispiel #1
0
        private void DisplayNonModal(ObservableObject viewModel, string title)
        {
            Type viewModelType = viewModel.GetType();
            PopupAssociatedViewAttribute attribute = viewModelType.GetCustomAttributes <PopupAssociatedViewAttribute>().FirstOrDefault();

            if (attribute == null)
            {
                throw new InvalidOperationException($"PopupAssociatedViewAttribute not found on {viewModelType}.");
            }
            FrameworkElement view = Activator.CreateInstance(attribute.ViewType) as FrameworkElement;

            if (view == null)
            {
                throw new InvalidOperationException($"Cannot create {attribute.ViewType} instance.");
            }
            view.DataContext = viewModel;
            PopupWindow modalPopupWindow = new PopupWindow
            {
                Owner = _mainWindow,
                Title = title,
                PopupContentPresenter =
                {
                    Content = view
                }
            };

            modalPopupWindow.Closed += (sender, args) => _mainWindow.IsEnabled = true;
            _mainWindow.IsEnabled    = false;
            modalPopupWindow.Show(); // TODO: ShowDialog ?
        }
Beispiel #2
0
        private FrameworkElement CreatePopupView(ObservableObject viewModel)
        {
            Type viewModelType = viewModel.GetType();

            // Search in registered view-viewmodel
            if (!_associatedViewsByViewModels.TryGetValue(viewModelType, out var viewType))
            // if not found in registered view-viewmodel, search attribute
            {
                PopupAssociatedViewAttribute attribute = viewModelType.GetCustomAttributes <PopupAssociatedViewAttribute>().FirstOrDefault();
                if (attribute == null)
                {
                    throw new InvalidOperationException($"PopupAssociatedView attribute not found on {viewModelType}.");
                }
                //// Search ClosePopupCommandAttribute and hook command
                //Type iCommandType = typeof(ICommand);
                //IEnumerable<FieldInfo> closeCommandFields = viewModelType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttributes<ClosePopupCommandAttribute>().Any() && x.FieldType == iCommandType); // TODO: inheriting from ICommand ?
                //foreach (FieldInfo fieldInfo in closeCommandFields)
                //{
                //    ClosePopupCommandAttribute closePopupCommandAttribute = fieldInfo.GetCustomAttributes<ClosePopupCommandAttribute>().FirstOrDefault();
                //    PropertyInfo relatedProperty = viewModelType.GetProperty(closePopupCommandAttribute.RelatedProperty);
                //    ICommand originalCommand = relatedProperty.GetValue(viewModel) as ICommand;
                //    // TODO: check if RelayCommand<T>
                //    ICommand closedCommand = new RelayCommand<object>(o =>
                //    {
                //        Close(viewModel);
                //        originalCommand.Execute(o);
                //    });
                //    fieldInfo.SetValue(viewModel, closedCommand);
                //}
                //
                viewType = attribute.ViewType;
            }
            FrameworkElement view = Activator.CreateInstance(viewType) as FrameworkElement;

            if (view == null)
            {
                throw new InvalidOperationException($"Popup {viewType} instance is not a valid FrameworkElement.");
            }
            view.DataContext = viewModel;
            return(view);
        }