Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleDataWindow" /> class.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="mode">The data window mode.</param>
        /// <param name="additionalButtons">The additional buttons.</param>
        /// <exception cref="System.NotSupportedException"></exception>
        protected SimpleDataWindow(IViewModel viewModel, DataWindowMode mode = DataWindowMode.OkCancel, IEnumerable <DataWindowButton> additionalButtons = null)
        {
            if (CatelEnvironment.IsInDesignMode)
            {
                return;
            }

            ThemeHelper.EnsureCatelMvvmThemeIsLoaded();

            _logic = new WindowLogic(this, null, viewModel);
            _logic.PropertyChanged          += (sender, e) => PropertyChanged.SafeInvoke(this, e);
            _logic.ViewModelChanged         += (sender, e) => ViewModelChanged.SafeInvoke(this, e);
            _logic.ViewModelPropertyChanged += (sender, e) => ViewModelPropertyChanged.SafeInvoke(this, e);

            Loaded             += (sender, e) => _viewLoaded.SafeInvoke(this);
            Unloaded           += (sender, e) => _viewUnloaded.SafeInvoke(this);
            DataContextChanged += (sender, e) => _viewDataContextChanged.SafeInvoke(this, new DataContextChangedEventArgs(e.OldValue, e.NewValue));

            if (additionalButtons != null)
            {
                foreach (var button in additionalButtons)
                {
                    _buttons.Add(button);
                }
            }

            var languageService = ServiceLocator.Default.ResolveType <ILanguageService>();

            if (mode == DataWindowMode.OkCancel || mode == DataWindowMode.OkCancelApply)
            {
                var button = DataWindowButton.FromAsync(languageService.GetString("OK"), OnOkExecuteAsync, OnOkCanExecute);
                button.IsDefault = true;
                _buttons.Add(button);
            }

            if (mode == DataWindowMode.OkCancel || mode == DataWindowMode.OkCancelApply)
            {
                var button = DataWindowButton.FromAsync(languageService.GetString("Cancel"), OnCancelExecuteAsync, OnCancelCanExecute);
                button.IsCancel = true;
                _buttons.Add(button);
            }

            if (mode == DataWindowMode.OkCancelApply)
            {
                var button = DataWindowButton.FromSync(languageService.GetString("Apply"), OnApplyExecute, OnApplyCanExecute);
                _buttons.Add(button);
            }

            if (mode == DataWindowMode.Close)
            {
                var button = DataWindowButton.FromSync(languageService.GetString("Close"), OnCloseExecute, null);
                _buttons.Add(button);
            }

            // Call manually the first time (for injected view models)
            OnViewModelChanged();

            this.FixBlurriness();
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataWindow"/> class.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="mode"><see cref="DataWindowMode"/>.</param>
        /// <param name="additionalButtons">The additional buttons.</param>
        /// <param name="defaultButton">The default button.</param>
        /// <param name="setOwnerAndFocus">if set to <c>true</c>, set the main window as owner window and focus the window.</param>
        /// <param name="infoBarMessageControlGenerationMode">The info bar message control generation mode.</param>
        public MetroDataWindow(IViewModel viewModel, DataWindowMode mode, IEnumerable <DataWindowButton> additionalButtons = null,
                               DataWindowDefaultButton defaultButton = DataWindowDefaultButton.OK, bool setOwnerAndFocus = true,
                               InfoBarMessageControlGenerationMode infoBarMessageControlGenerationMode = InfoBarMessageControlGenerationMode.Inline)
        {
            if (CatelEnvironment.IsInDesignMode)
            {
                return;
            }

            Mode          = mode;
            DefaultButton = defaultButton;
            _infoBarMessageControlGenerationMode = infoBarMessageControlGenerationMode;

            this.FixBlurriness();

            SizeToContent         = SizeToContent.WidthAndHeight;
            ShowInTaskbar         = false;
            ResizeMode            = ResizeMode.NoResize;
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            BorderThickness       = new Thickness(1d);
            BorderBrush           = Orchestra.ThemeHelper.GetAccentColorBrush();

            this.ApplyIconFromApplication();

            ThemeHelper.EnsureCatelMvvmThemeIsLoaded();

            _logic = new WindowLogic(this, null, viewModel);
            _logic.TargetViewPropertyChanged += (sender, e) =>
            {
                // Do not call this for ActualWidth and ActualHeight WPF, will cause problems with NET 40
                // on systems where NET45 is *not* installed
                if (!string.Equals(e.PropertyName, "ActualWidth", StringComparison.InvariantCulture) &&
                    !string.Equals(e.PropertyName, "ActualHeight", StringComparison.InvariantCulture))
                {
                    PropertyChanged.SafeInvoke(this, e);
                }
            };

            _logic.ViewModelClosedAsync += OnViewModelClosedAsync;
            _logic.ViewModelChanged     += (sender, e) => RaiseViewModelChanged();

            _logic.ViewModelPropertyChanged += (sender, e) =>
            {
                OnViewModelPropertyChanged(sender, e);

                ViewModelPropertyChanged.SafeInvoke(this, e);
            };

            Loaded += (sender, e) =>
            {
                _viewLoaded.SafeInvoke(this);

                OnLoaded(e);
            };

            Unloaded += (sender, e) =>
            {
                _viewUnloaded.SafeInvoke(this);

                OnUnloaded(e);
            };

            SetBinding(TitleProperty, new Binding("Title"));

            if (additionalButtons != null)
            {
                foreach (var button in additionalButtons)
                {
                    _buttons.Add(button);
                }
            }

            CanClose            = true;
            CanCloseUsingEscape = true;

            Loaded             += (sender, e) => Initialize();
            Closing            += OnDataWindowClosing;
            DataContextChanged += (sender, e) => _viewDataContextChanged.SafeInvoke(this, new DataContextChangedEventArgs(e.OldValue, e.NewValue));

            if (setOwnerAndFocus)
            {
                this.SetOwnerWindowAndFocus();
            }
            else
            {
                this.FocusFirstControl();
            }
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DataWindow"/> class.
        /// </summary>
        /// <param name="viewModel">The view model.</param>
        /// <param name="mode"><see cref="DataWindowMode"/>.</param>
        /// <param name="additionalButtons">The additional buttons.</param>
        /// <param name="defaultButton">The default button.</param>
        /// <param name="setOwnerAndFocus">if set to <c>true</c>, set the main window as owner window and focus the window.</param>
        /// <param name="infoBarMessageControlGenerationMode">The info bar message control generation mode.</param>
        /// <param name="focusFirstControl">if set to <c>true</c>, the first control will get the focus.</param>
        public DataWindow(IViewModel viewModel, DataWindowMode mode, IEnumerable <DataWindowButton> additionalButtons = null,
                          DataWindowDefaultButton defaultButton = DataWindowDefaultButton.OK, bool setOwnerAndFocus = true,
                          InfoBarMessageControlGenerationMode infoBarMessageControlGenerationMode = InfoBarMessageControlGenerationMode.Inline, bool focusFirstControl = true)
        {
            if (CatelEnvironment.IsInDesignMode)
            {
                return;
            }

            // Set window style (WPF doesn't allow styling on root elements of XAML files, too bad)
            // For more info, see http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3059c0e4-c372-4da2-b384-28f271feef05/
            SetResourceReference(StyleProperty, typeof(DataWindow));

            Mode          = mode;
            DefaultButton = defaultButton;
            _infoBarMessageControlGenerationMode = infoBarMessageControlGenerationMode;

            this.FixBlurriness();

            SizeToContent         = SizeToContent.WidthAndHeight;
            ShowInTaskbar         = false;
            ResizeMode            = ResizeMode.NoResize;
            WindowStartupLocation = WindowStartupLocation.CenterOwner;

            this.ApplyIconFromApplication();

            ThemeHelper.EnsureCatelMvvmThemeIsLoaded();

            _logic = new WindowLogic(this, null, viewModel);
            _logic.TargetViewPropertyChanged += (sender, e) =>
            {
                // Do not call this for ActualWidth and ActualHeight WPF, will cause problems with NET 40
                // on systems where NET45 is *not* installed
                if (!string.Equals(e.PropertyName, nameof(ActualWidth), StringComparison.InvariantCulture) &&
                    !string.Equals(e.PropertyName, nameof(ActualHeight), StringComparison.InvariantCulture))
                {
                    PropertyChanged?.Invoke(this, e);
                }
            };

            _logic.ViewModelClosedAsync += OnViewModelClosedAsync;
            _logic.ViewModelChanged     += (sender, e) => RaiseViewModelChanged();

            _logic.ViewModelPropertyChanged += (sender, e) =>
            {
                OnViewModelPropertyChanged(sender, e);

                ViewModelPropertyChanged?.Invoke(this, e);
            };

            Loaded += (sender, e) =>
            {
                _viewLoaded?.Invoke(this, EventArgs.Empty);

                OnLoaded(e);
            };

            Unloaded += (sender, e) =>
            {
                _viewUnloaded?.Invoke(this, EventArgs.Empty);

                OnUnloaded(e);
            };

            SetBinding(TitleProperty, new Binding("Title"));

            if (additionalButtons != null)
            {
                foreach (var button in additionalButtons)
                {
                    _buttons.Add(button);
                }
            }

            CanClose            = true;
            CanCloseUsingEscape = true;

            Loaded             += (sender, e) => Initialize();
            DataContextChanged += (sender, e) => _viewDataContextChanged?.Invoke(this, new DataContextChangedEventArgs(e.OldValue, e.NewValue));

            // #1150 Subscribe in dispatcher to allow derived types to be the first handler
            Dispatcher.BeginInvoke(() =>
            {
                Closing += OnDataWindowClosing;
            });

            _focusFirstControl = focusFirstControl;

            if (setOwnerAndFocus)
            {
                this.SetOwnerWindowAndFocus(focusFirstControl: focusFirstControl);
            }
            else if (focusFirstControl)
            {
                this.FocusFirstControl();
            }
        }