/// <summary> /// Called when the <c>TargetView</c> has just been loaded. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> public override async Task OnTargetViewLoadedAsync(object sender, EventArgs e) { await CompleteViewModelClosingAsync(); // Do not call base because it will create a VM. We will create the VM ourselves //base.OnTargetControlLoaded(sender, e); // Manually updating target control content wrapper here (not by content property changed event handler), // because in WinRT UserControl does NOT update bindings while InitializeComponents() method is executing, // even if the Content property was changed while InitializeComponents() running there is no triggering of a binding update. CreateViewModelWrapper(); #if NET if (!SkipSearchingForInfoBarMessageControl) { Log.Debug("Searching for an instance of the InfoBarMessageControl"); _infoBarMessageControl = TargetView.FindParentByPredicate(o => o is InfoBarMessageControl) as InfoBarMessageControl; ApiCop.UpdateRule <UnusedFeatureApiCopRule>("UserControlLogic.InfoBarMessageControl", rule => rule.IncreaseCount(_infoBarMessageControl != null, TargetViewType.FullName)); if (CreateWarningAndErrorValidatorForViewModel) { ApiCop.UpdateRule <UnusedFeatureApiCopRule>("UserControlLogic.CreateWarningAndErrorValidator", rule => rule.IncreaseCount(_infoBarMessageControl != null, TargetViewType.FullName)); } Log.Debug("Finished searching for an instance of the InfoBarMessageControl"); if (_infoBarMessageControl == null) { Log.Warning("No InfoBarMessageControl is found in the visual tree of '{0}', consider using the SkipSearchingForInfoBarMessageControl property to improve performance", GetType().Name); } } else { Log.Debug("Skipping the search for an instance of the InfoBarMessageControl"); } #endif if (!CloseViewModelOnUnloaded && (ViewModel != null)) { // Re-use view model Log.Debug("Re-using existing view model"); } if (ViewModel == null) { // Try to create view model based on data context await UpdateDataContextToUseViewModelAsync(TargetView.DataContext); } if (DisableWhenNoViewModel) { TargetView.IsEnabled = (ViewModel != null); } }
/// <summary> /// Called when the <see cref="TargetControl"/> has just been loaded. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> public override void OnTargetControlLoaded(object sender, UIEventArgs e) { // Do not call base because it will create a VM. We will create the VM ourselves //base.OnTargetControlLoaded(sender, e); // Manually updating target control content wrapper here (not by content property changed event handler), // because in WinRT UserControl does NOT update bindings while InitializeComponents() method is executing, // even if the Content property was changed while InitializeComponents() running there is no triggering of a binding update. CreateViewModelGrid(); #if NET || SL4 || SL5 if (!SkipSearchingForInfoBarMessageControl) { Log.Debug("Searching for an instance of the InfoBarMessageControl"); _infoBarMessageControl = FindParentByPredicate(TargetControl, o => o is InfoBarMessageControl) as InfoBarMessageControl; Log.Debug("Finished searching for an instance of the InfoBarMessageControl"); if (_infoBarMessageControl == null) { Log.Warning("No InfoBarMessageControl is found in the visual tree of '{0}', consider using the SkipSearchingForInfoBarMessageControl property to improve performance", GetType().Name); } } else { Log.Debug("Skipping the search for an instance of the InfoBarMessageControl"); } #endif if (!CloseViewModelOnUnloaded && (ViewModel != null)) { // Re-use view model Log.Debug("Re-using existing view model"); } if (ViewModel == null) { // Try to create view model based on data context ViewModel = CreateViewModelByUsingDataContextOrConstructor(); } if (DisableWhenNoViewModel) { TargetControl.IsEnabled = (ViewModel != null); } }
/// <summary> /// Called when the <see cref="TargetControl"/> has just been loaded. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> public override void OnTargetControlLoaded(object sender, UIEventArgs e) { // Do not call base because it will create a VM. We will create the VM ourselves //base.OnTargetControlLoaded(sender, e); #if NET || SL4 || SL5 if (!SkipSearchingForInfoBarMessageControl) { Log.Debug("Searching for an instance of the InfoBarMessageControl"); _infoBarMessageControl = FindParentByPredicate(TargetControl, o => o is InfoBarMessageControl) as InfoBarMessageControl; Log.Debug("Finished searching for an instance of the InfoBarMessageControl"); if (_infoBarMessageControl == null) { Log.Warning("No InfoBarMessageControl is found in the visual tree of '{0}', consider using the SkipSearchingForInfoBarMessageControl property to improve performance", GetType().Name); } } else { Log.Debug("Skipping the search for an instance of the InfoBarMessageControl"); } #endif if (!CloseViewModelOnUnloaded && (ViewModel != null)) { // Re-use view model Log.Debug("Re-using existing view model"); } if (ViewModel == null) { // Try to create view model based on data context ViewModel = CreateViewModelByUsingDataContextOrConstructor(); } if (DisableWhenNoViewModel) { TargetControl.IsEnabled = (ViewModel != null); } }
/// <summary> /// Wraps the specified framework element. /// </summary> /// <param name="frameworkElement">The framework element.</param> /// <param name="wrapOptions">The wrap options.</param> /// <param name="buttons">The buttons to add.</param> /// <param name="parentContentControl">The parent content control.</param> /// <returns><see cref="Grid"/> that contains the wrapped content.</returns> /// <remarks> /// The framework element that is passed must be disconnected from the parent first. It is recommended to first check whether a /// framework element can be wrapped by using the <see cref="CanBeWrapped"/> method. /// This method will automatically handle the disconnecting of the framework element from the parent is the <paramref name="parentContentControl"/> /// is passed. /// </remarks> private static Grid Wrap(FrameworkElement frameworkElement, WrapOptions wrapOptions, DataWindowButton[] buttons, object parentContentControl) { Argument.IsNotNull("frameworkElement", frameworkElement); Argument.IsNotNull("buttons", buttons); if (frameworkElement.Name == MainContentHolderName) { return((Grid)frameworkElement); } #if SILVERLIGHT // According to the documentation, no visual tree is garantueed in the Loaded event of the user control. // However, as a solution the documentation says you need to manually call ApplyTemplate, so let's do that. // For more info, see http://msdn.microsoft.com/en-us/library/ms596558(vs.95) var frameworkElementAsControl = frameworkElement as Control; if (frameworkElementAsControl != null) { frameworkElementAsControl.ApplyTemplate(); } #endif if (parentContentControl != null) { SetControlContent(parentContentControl, null); } FrameworkElement mainContent = frameworkElement; // Create the outside grid, so the inner grid is never the same as the main content holder var outsideGrid = new Grid(); outsideGrid.Name = MainContentHolderName; if (Application.Current != null) { #if SILVERLIGHT // TODO: Fix styles for silverlight #else outsideGrid.Resources.MergedDictionaries.Add(Application.Current.Resources); #endif } #region Generate buttons #if !NETFX_CORE if (buttons.Length > 0) { // Add wrappanel containing the buttons var buttonsWrapPanel = new WrapPanel(); buttonsWrapPanel.Name = ButtonsWrapPanelName; #if SILVERLIGHT buttonsWrapPanel.Style = Application.Current.Resources["DataWindowButtonContainerStyle"] as Style; #else buttonsWrapPanel.SetResourceReference(FrameworkElement.StyleProperty, "DataWindowButtonContainerStyle"); #endif foreach (var dataWindowButton in buttons) { var button = new Button(); if (dataWindowButton.CommandBindingPath != null) { button.SetBinding(ButtonBase.CommandProperty, new Binding(dataWindowButton.CommandBindingPath)); } else { button.Command = dataWindowButton.Command; } button.Content = dataWindowButton.Text; #if NET button.SetResourceReference(FrameworkElement.StyleProperty, "DataWindowButtonStyle"); button.IsDefault = dataWindowButton.IsDefault; button.IsCancel = dataWindowButton.IsCancel; #else button.Style = Application.Current.Resources["DataWindowButtonStyle"] as Style; #endif if (dataWindowButton.IsDefault) { button.Name = DefaultOkButtonName; } else if (dataWindowButton.IsCancel) { button.Name = DefaultCancelButtonName; } buttonsWrapPanel.Children.Add(button); } // Create dockpanel that will dock the buttons underneath the content var subDockPanel = new DockPanel(); subDockPanel.LastChildFill = true; DockPanel.SetDock(buttonsWrapPanel, Dock.Bottom); subDockPanel.Children.Add(buttonsWrapPanel); // Add actual content subDockPanel.Children.Add(frameworkElement); // The dockpanel is now the main content mainContent = subDockPanel; } #endif #endregion #region Generate internal grid // Create grid var internalGrid = new Grid(); internalGrid.Name = InternalGridName; internalGrid.Children.Add(mainContent); // Grid is now the main content mainContent = internalGrid; #endregion #region Generate WarningAndErrorValidator if (Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.GenerateWarningAndErrorValidatorForDataContext)) { // Create warning and error validator var warningAndErrorValidator = new WarningAndErrorValidator(); warningAndErrorValidator.Name = WarningAndErrorValidatorName; warningAndErrorValidator.SetBinding(WarningAndErrorValidator.SourceProperty, new Binding()); // Add to grid internalGrid.Children.Add(warningAndErrorValidator); } #endregion #region Generate InfoBarMessageControl #if !NETFX_CORE if (Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.GenerateInlineInfoBarMessageControl) || Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.GenerateOverlayInfoBarMessageControl)) { // Create info bar message control var infoBarMessageControl = new InfoBarMessageControl(); infoBarMessageControl.Name = InfoBarMessageControlName; infoBarMessageControl.Content = mainContent; if (Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.GenerateOverlayInfoBarMessageControl)) { infoBarMessageControl.Mode = InfoBarMessageControlMode.Overlay; } // This is now the main content mainContent = infoBarMessageControl; } #endif #endregion // Set content of the outside grid outsideGrid.Children.Add(mainContent); if (parentContentControl != null) { SetControlContent(parentContentControl, outsideGrid); } return(outsideGrid); }
/// <summary> /// Called when the <c>TargetView</c> has just been loaded. /// </summary> /// <param name="sender">The sender.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> public override async void OnTargetViewLoaded(object sender, EventArgs e) { // Do not call base because it will create a VM. We will create the VM ourselves //base.OnTargetControlLoaded(sender, e); // Manually updating target control content wrapper here (not by content property changed event handler), // because in WinRT UserControl does NOT update bindings while InitializeComponents() method is executing, // even if the Content property was changed while InitializeComponents() running there is no triggering of a binding update. CreateViewModelWrapper(); #if NET || SL5 if (!SkipSearchingForInfoBarMessageControl) { Log.Debug("Searching for an instance of the InfoBarMessageControl"); _infoBarMessageControl = TargetView.FindParentByPredicate(o => o is InfoBarMessageControl) as InfoBarMessageControl; ApiCop.UpdateRule<UnusedFeatureApiCopRule>("UserControlLogic.InfoBarMessageControl", rule => rule.IncreaseCount(_infoBarMessageControl != null, TargetViewType.FullName)); if (CreateWarningAndErrorValidatorForViewModel) { ApiCop.UpdateRule<UnusedFeatureApiCopRule>("UserControlLogic.CreateWarningAndErrorValidator", rule => rule.IncreaseCount(_infoBarMessageControl != null, TargetViewType.FullName)); } Log.Debug("Finished searching for an instance of the InfoBarMessageControl"); if (_infoBarMessageControl == null) { Log.Warning("No InfoBarMessageControl is found in the visual tree of '{0}', consider using the SkipSearchingForInfoBarMessageControl property to improve performance", GetType().Name); } } else { Log.Debug("Skipping the search for an instance of the InfoBarMessageControl"); } #endif if (!CloseViewModelOnUnloaded && (ViewModel != null)) { // Re-use view model Log.Debug("Re-using existing view model"); } if (ViewModel == null) { // Try to create view model based on data context await UpdateDataContextToUseViewModelAsync(TargetView.DataContext); } if (DisableWhenNoViewModel) { TargetView.IsEnabled = (ViewModel != null); } }
/// <summary> /// Wraps the specified framework element. /// </summary> /// <param name="frameworkElement">The framework element.</param> /// <param name="wrapOptions">The wrap options.</param> /// <param name="buttons">The buttons to add.</param> /// <param name="parentContentControl">The parent content control.</param> /// <returns><see cref="Grid"/> that contains the wrapped content.</returns> /// <remarks> /// The framework element that is passed must be disconnected from the parent first. It is recommended to first check whether a /// framework element can be wrapped by using the <see cref="CanBeWrapped"/> method. /// This method will automatically handle the disconnecting of the framework element from the parent is the <paramref name="parentContentControl"/> /// is passed. /// </remarks> public Grid Wrap(FrameworkElement frameworkElement, WrapControlServiceWrapOptions wrapOptions, DataWindowButton[] buttons, ContentControl parentContentControl) { Argument.IsNotNull("frameworkElement", frameworkElement); Argument.IsNotNull("buttons", buttons); if (!string.IsNullOrWhiteSpace(frameworkElement.Name)) { if (frameworkElement.Name.StartsWith(WrapControlServiceControlNames.MainContentHolderName)) { return((Grid)frameworkElement); } } if (parentContentControl != null) { SetControlContent(parentContentControl, null); } var mainContent = frameworkElement; // Create the outside grid, so the inner grid is never the same as the main content holder var outsideGrid = new Grid(); outsideGrid.Name = WrapControlServiceControlNames.MainContentHolderName.GetUniqueControlName(); if (Application.Current != null) { outsideGrid.Resources.MergedDictionaries.Add(Application.Current.Resources); } #region Generate buttons #if !NETFX_CORE if (buttons.Length > 0) { // Add wrappanel containing the buttons var buttonsWrapPanel = new WrapPanel(); buttonsWrapPanel.Name = WrapControlServiceControlNames.ButtonsWrapPanelName; #if SILVERLIGHT buttonsWrapPanel.Style = Application.Current.Resources["DataWindowButtonContainerStyle"] as Style; #else buttonsWrapPanel.SetResourceReference(FrameworkElement.StyleProperty, "DataWindowButtonContainerStyle"); #endif foreach (var dataWindowButton in buttons) { var button = new Button(); if (dataWindowButton.CommandBindingPath != null) { button.SetBinding(ButtonBase.CommandProperty, new Binding(dataWindowButton.CommandBindingPath)); } else { button.Command = dataWindowButton.Command; } if (dataWindowButton.ContentBindingPath != null) { Binding contentBinding = new Binding(dataWindowButton.ContentBindingPath); if (dataWindowButton.ContentValueConverter != null) { contentBinding.Converter = dataWindowButton.ContentValueConverter; } button.SetBinding(ButtonBase.ContentProperty, contentBinding); } else { button.Content = dataWindowButton.Text; } if (dataWindowButton.VisibilityBindingPath != null) { Binding visibilityBinding = new Binding(dataWindowButton.VisibilityBindingPath); if (dataWindowButton.VisibilityValueConverter != null) { visibilityBinding.Converter = dataWindowButton.VisibilityValueConverter; } button.SetBinding(ButtonBase.VisibilityProperty, visibilityBinding); } #if NET button.SetResourceReference(FrameworkElement.StyleProperty, "DataWindowButtonStyle"); button.IsDefault = dataWindowButton.IsDefault; button.IsCancel = dataWindowButton.IsCancel; #else button.Style = Application.Current.Resources["DataWindowButtonStyle"] as Style; #endif if (dataWindowButton.IsDefault) { button.Name = WrapControlServiceControlNames.DefaultOkButtonName; } else if (dataWindowButton.IsCancel) { button.Name = WrapControlServiceControlNames.DefaultCancelButtonName; } buttonsWrapPanel.Children.Add(button); } // Create dockpanel that will dock the buttons underneath the content var subDockPanel = new DockPanel(); subDockPanel.LastChildFill = true; DockPanel.SetDock(buttonsWrapPanel, Dock.Bottom); subDockPanel.Children.Add(buttonsWrapPanel); // Add actual content subDockPanel.Children.Add(frameworkElement); // The dockpanel is now the main content mainContent = subDockPanel; } #endif #endregion #region Generate internal grid // Create grid var internalGrid = new Grid(); internalGrid.Name = WrapControlServiceControlNames.InternalGridName; internalGrid.Children.Add(mainContent); // Grid is now the main content mainContent = internalGrid; #endregion #region Generate WarningAndErrorValidator if (Enum <WrapControlServiceWrapOptions> .Flags.IsFlagSet(wrapOptions, WrapControlServiceWrapOptions.GenerateWarningAndErrorValidatorForDataContext)) { // Create warning and error validator var warningAndErrorValidator = new WarningAndErrorValidator(); warningAndErrorValidator.Name = WrapControlServiceControlNames.WarningAndErrorValidatorName; warningAndErrorValidator.SetBinding(WarningAndErrorValidator.SourceProperty, new Binding()); // Add to grid internalGrid.Children.Add(warningAndErrorValidator); } #endregion #region Generate InfoBarMessageControl #if !NETFX_CORE if (Enum <WrapControlServiceWrapOptions> .Flags.IsFlagSet(wrapOptions, WrapControlServiceWrapOptions.GenerateInlineInfoBarMessageControl) || Enum <WrapControlServiceWrapOptions> .Flags.IsFlagSet(wrapOptions, WrapControlServiceWrapOptions.GenerateOverlayInfoBarMessageControl)) { // Create info bar message control var infoBarMessageControl = new InfoBarMessageControl(); infoBarMessageControl.Name = WrapControlServiceControlNames.InfoBarMessageControlName; infoBarMessageControl.Content = mainContent; if (Enum <WrapControlServiceWrapOptions> .Flags.IsFlagSet(wrapOptions, WrapControlServiceWrapOptions.GenerateOverlayInfoBarMessageControl)) { infoBarMessageControl.Mode = InfoBarMessageControlMode.Overlay; } // This is now the main content mainContent = infoBarMessageControl; } #endif #endregion // Set content of the outside grid outsideGrid.Children.Add(mainContent); if (parentContentControl != null) { SetControlContent(parentContentControl, outsideGrid); } return(outsideGrid); }