/// <summary> /// Creates the grid that contains the view model as a data context. /// </summary> private void CreateViewModelGrid() { var content = TargetControl.Content as FrameworkElement; if (content == null) { return; } Log.Debug("Creating view model grid that will serve as view model container"); _viewModelGrid = new Grid(); _viewModelGrid.SetBinding(FrameworkElement.DataContextProperty, new Binding { Path = new PropertyPath("ViewModel"), Source = this }); #if NET || SL4 || SL5 if (CreateWarningAndErrorValidatorForViewModel) { var warningAndErrorValidator = new WarningAndErrorValidator(); warningAndErrorValidator.SetBinding(WarningAndErrorValidator.SourceProperty, new Binding()); _viewModelGrid.Children.Add(warningAndErrorValidator); } #endif TargetControl.Content = null; _viewModelGrid.Children.Add(content); TargetControl.Content = _viewModelGrid; Log.Debug("Created view model grid"); }
private IViewModelWrapper CreateViewModelGrid(IView view, object viewModelSource, WrapOptions wrapOptions) { var content = GetContent(view) as FrameworkElement; if (content == null) { return(null); } object tempObj = null; if (_weakIsWrappingTable.TryGetValue(view, out tempObj)) { return(null); } _weakIsWrappingTable.Add(view, new object()); var vmGrid = new Grid(); vmGrid.Name = InnerWrapperName; vmGrid.SetBinding(FrameworkElement.DataContextProperty, new Binding { Path = new PropertyPath("ViewModel"), Source = viewModelSource }); #if NET || SL5 if (Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.CreateWarningAndErrorValidatorForViewModel)) { var warningAndErrorValidator = new WarningAndErrorValidator(); warningAndErrorValidator.SetBinding(WarningAndErrorValidator.SourceProperty, new Binding()); vmGrid.Children.Add(warningAndErrorValidator); } #endif if (Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.TransferStylesAndTransitionsToViewModelGrid)) { content.TransferStylesAndTransitions(vmGrid); } SetContent(view, null); vmGrid.Children.Add(content); SetContent(view, vmGrid); Log.Debug("Created target control content wrapper grid for view model"); return(new ViewModelWrapper(vmGrid)); }
/// <summary> /// Creates the target control content wrapper grid that contains the view model as a data context. /// </summary> private void CreateViewModelGrid() { var update = new Action(() => { var content = TargetControl.Content as FrameworkElement; if (content == null || ReferenceEquals(content, _viewModelGrid)) { return; } Log.Debug("Creating target control content wrapper grid that will serve as view model container."); _viewModelGrid = new Grid(); _viewModelGrid.SetBinding(FrameworkElement.DataContextProperty, new Binding { Path = new PropertyPath("ViewModel"), Source = this }); #if NET || SL4 || SL5 if (CreateWarningAndErrorValidatorForViewModel) { var warningAndErrorValidator = new WarningAndErrorValidator(); warningAndErrorValidator.SetBinding(WarningAndErrorValidator.SourceProperty, new Binding()); _viewModelGrid.Children.Add(warningAndErrorValidator); } #endif TargetControl.Content = null; _viewModelGrid.Children.Add(content); TargetControl.Content = _viewModelGrid; Log.Debug("Created target control content wrapper grid for view model."); }); // NOTE: Beginning invoke (running async) because setting of TargetControl Content property causes memory faults // when this method called by TargetControlContentChanged handler. #if NETFX_CORE TargetControl.Dispatcher.RunAsync(CoreDispatcherPriority.High, () => update()); #else update(); #endif }
/// <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); }
private IViewModelWrapper CreateViewModelGrid(IView view, object viewModelSource, WrapOptions wrapOptions) { var content = GetContent(view) as FrameworkElement; if (content == null) { return(null); } object tempObj = null; if (_weakIsWrappingTable.TryGetValue(view, out tempObj)) { return(null); } var viewTypeName = view.GetType().Name; _weakIsWrappingTable.Add(view, new object()); Grid vmGrid = null; var existingGrid = GetContent(view) as Grid; if (existingGrid != null) { if (existingGrid.Name.StartsWith(InnerWrapperName)) { Log.Debug($"No need to create content wrapper grid for view model for view '{viewTypeName}', custom grid with special name defined"); vmGrid = existingGrid; } } if (vmGrid == null) { Log.Debug($"Creating content wrapper grid for view model for view '{viewTypeName}'"); vmGrid = new Grid(); vmGrid.Name = InnerWrapperName.GetUniqueControlName(); #if NET if (Enum <WrapOptions> .Flags.IsFlagSet(wrapOptions, WrapOptions.CreateWarningAndErrorValidatorForViewModel)) { var warningAndErrorValidator = new WarningAndErrorValidator(); warningAndErrorValidator.SetBinding(WarningAndErrorValidator.SourceProperty, new Binding()); vmGrid.Children.Add(warningAndErrorValidator); } #endif SetContent(view, null); vmGrid.Children.Add(content); SetContent(view, vmGrid); Log.Debug($"Created content wrapper grid for view model for view '{viewTypeName}'"); } var binding = vmGrid.GetBindingExpression(FrameworkElement.DataContextProperty); if (binding == null) { vmGrid.SetBinding(FrameworkElement.DataContextProperty, new Binding { Path = new PropertyPath("ViewModel"), Source = viewModelSource }); } return(new ViewModelWrapper(vmGrid)); }
/// <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); }