/// <summary>
 /// Displays content in a window
 /// </summary>
 /// <param name="windowTitle">Title text shown in window</param>
 /// <param name="windowContents">Contents of the window</param>        
 /// <param name="isModal">Determines wheter the window is modal or not</param>
 /// <param name="onClosingHandler">Event handler invoked when window is closing, and can be handled to cancel window closure</param>
 /// <param name="onClosedHandler">Event handler invoked when window is closed</param>
 public void ShowWindow(string windowTitle, FrameworkElement windowContents, bool isModal = false, EventHandler<CancelEventArgs> onClosingHandler = null, EventHandler onClosedHandler = null)            
 {                        
     if (windowContents == null)
         throw new ArgumentNullException("windowContents");
     
     int hashCode = windowContents.GetHashCode();
     ChildWindow childWindow = null;
     if (!m_ChildWindows.TryGetValue(hashCode, out childWindow))
     {
         // not existing yet
         childWindow = new ChildWindow()
         {
             Title = windowTitle ?? string.Empty,
         };                
         if (onClosedHandler != null)
             childWindow.Closed += onClosedHandler;
         if (onClosingHandler != null)
             childWindow.Closing += onClosingHandler;
         childWindow.CloseCompleted += new EventHandler(childWindow_CloseCompleted);
         childWindow.Content = windowContents;
         m_ChildWindows.Add(hashCode, childWindow);
         if (!isModal) 
         { 
             windowContents.Loaded += (o, e) => {
                 DialogResizeHelper.CenterAndSizeDialog(windowContents, childWindow);
             };
             DialogResizeHelper.CenterAndSizeDialog(windowContents, childWindow);
         }
     }
     childWindow.ShowDialog(isModal);
 }
예제 #2
0
        /// <summary>
        /// Starts tracking the provided ChildWindow.
        /// </summary>
        /// <param name="childWindow">The ChildWindow to be tracked.</param>
        public static void Track(FrameworkElement childWindow)
        {
            if (childWindow == null)
                throw new ArgumentNullException("childWindow");

            // Verify that the object is a ChildWindow or subclass
            Type windowType = childWindow.GetType();
            while (windowType != null && !String.Equals(windowType.FullName, "System.Windows.Controls.ChildWindow"))
                windowType = windowType.BaseType;

            if (windowType == null)
                throw new ArgumentException("Tracked element is not a subclass of ChildWindow", "childWindow");

            if (!s_childWindows.ContainsKey(childWindow.GetHashCode()))
                s_childWindows.Add(childWindow.GetHashCode(), new WeakReference(childWindow));
        }
        /// <summary>
        /// Displays content in a window
        /// </summary>
        /// <param name="windowTitle">Title text shown in window</param>
        /// <param name="windowContents">Contents of the window</param>        
        /// <param name="isModal">Determines wheter the window is modal or not</param>
        /// <param name="onClosingHandler">Event handler invoked when window is closing, and can be handled to cancel window closure</param>
        /// <param name="windowType">The type of the window</param>
        /// <param name="onClosedHandler">Event handler invoked when window is closed</param>
        /// <param name="top">The distance from the top of the application at which to position the window</param>
        /// <param name="left">The distance from the left of the application at which to position the window</param>
        /// <returns>The window</returns>
        public object ShowWindow(string windowTitle, FrameworkElement windowContents, bool isModal = false, 
            EventHandler<CancelEventArgs> onClosingHandler = null, EventHandler onClosedHandler = null, 
            WindowType windowType = WindowType.Floating, double? top = null, double? left = null)            
        {                        
            if (windowContents == null)
                throw new ArgumentNullException("windowContents");

            int hashCode = windowContents.GetHashCode();
            FloatingWindow floatingWindow = null;
            if (!m_FloatingWindows.TryGetValue(hashCode, out floatingWindow))
            {
                // not existing yet
                floatingWindow = new FloatingWindow()
                {
                    Title = windowTitle ?? string.Empty,
                };

                switch (windowType)
                {
                    case WindowType.Floating:
                if (FloatingWindowStyle != null)
                    floatingWindow.Style = FloatingWindowStyle;
                        break;
                    case WindowType.DesignTimeFloating:
                        if (DesignTimeWindowStyle != null)
                            floatingWindow.Style = DesignTimeWindowStyle;
                        else if (FloatingWindowStyle != null) // fallback to FloatingWindowStyle
                            floatingWindow.Style = FloatingWindowStyle;
                        break;
                }

                floatingWindow.Closed += (o, e) =>
                {
                    if (onClosedHandler != null)
                        onClosedHandler.Invoke(o, e);

                    m_FloatingWindows.Remove(hashCode);

                    if (floatingWindow != null)
                        floatingWindow.Content = null;
                };

                if (onClosingHandler != null)
                    floatingWindow.Closing += onClosingHandler;
                floatingWindow.Content = windowContents;
                m_FloatingWindows.Add(hashCode, floatingWindow);
            }


            if (top != null)
                floatingWindow.VerticalOffset = (double)top;

            if (left != null)
                floatingWindow.HorizontalOffset = (double)left;

            floatingWindow.Show(isModal);

            return floatingWindow;
        }
        public string RegisterElement(FrameworkElement element)
        {
            var registeredKey = this.registeredElements.FirstOrDefault(x => x.Value.Target == element).Key;

            if (registeredKey == null)
            {
                Interlocked.Increment(ref safeInstanceCount);

                registeredKey = element.GetHashCode() + "-" + safeInstanceCount.ToString(string.Empty, CultureInfo.InvariantCulture);
                this.registeredElements.Add(registeredKey, new WeakReference(element));
            }

            return registeredKey;
        }
예제 #5
0
        private static BindingExpressionBase GetBindingExpressionBaseForPropertyName(
            FrameworkElement element,
            string dependencyPropertyName,
            bool thrownOnError)
        {
            BindingExpressionBase bindingExpressionBase = null;

            // Get the property to be validated
            var descriptor =
                DependencyPropertyDescriptor.FromName(dependencyPropertyName, element.GetType(), element.GetType());
            if (descriptor == null)
            {
                if (thrownOnError)
                {
                    throw new InvalidOperationException(
                        string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.ExceptionMissingDependencyProperty,
                            dependencyPropertyName,
                            element.GetType().Name,
                            element.Name,
                            element.GetHashCode()));
                }
            }
            else
            {
                var dependencyProperty = descriptor.DependencyProperty;

                // Get the BindingExpression for the property to validate
                bindingExpressionBase = BindingOperations.GetBindingExpressionBase(element, dependencyProperty);
                if (bindingExpressionBase == null)
                {
                    if (thrownOnError)
                    {
                        throw new InvalidOperationException(
                            string.Format(
                                CultureInfo.CurrentCulture,
                                Resources.ExceptionDependencyPropertyHasNoBinding,
                                dependencyPropertyName,
                                element.GetType().Name,
                                element.Name,
                                element.GetHashCode()));
                    }
                }
            }

            return bindingExpressionBase;
        }
예제 #6
0
        //  ===========================================================================
        //  These methods are invoked to during a call call to
        //  FE.EnsureVisual or FCE.EnsureLogical
        //  ===========================================================================

        #region InstantiateSubTree

        //
        //  This method
        //  Creates the VisualTree
        //
        //[CodeAnalysis("AptcaMethodsShouldOnlyCallAptcaMethods")] //Tracking Bug: 29647
        internal bool ApplyTemplateContent(
            UncommonField<HybridDictionary[]> templateDataField,
            FrameworkElement container)
        {
#if STYLE_TRACE
            _timer.Begin();
#endif

            if (TraceDependencyProperty.IsEnabled)
            {
                TraceDependencyProperty.Trace(
                    TraceEventType.Start,
                    TraceDependencyProperty.ApplyTemplateContent,
                    container,
                    this);
            }


            ValidateTemplatedParent(container);

            bool visualsCreated = StyleHelper.ApplyTemplateContent(templateDataField, container,
                _templateRoot, _lastChildIndex,
                ChildIndexFromChildName, this);

            if (TraceDependencyProperty.IsEnabled)
            {
                TraceDependencyProperty.Trace(
                    TraceEventType.Stop,
                    TraceDependencyProperty.ApplyTemplateContent,
                    container,
                    this);
            }


#if STYLE_TRACE
            _timer.End();
            if (visualsCreated)
            {
                string label = container.ID;
                if (label == null || label.Length == 0)
                    label = container.GetHashCode().ToString();
                Console.WriteLine("  Style.VT created for {0} {1} in {2:f2} msec",
                    container.GetType().Name, label, _timer.TimeOfLastPeriod);
            }
#endif

            return visualsCreated;
        }
        /// <summary>
        /// Hides the dialog window 
        /// </summary>
        /// <param name="windowContents">Contents of the window displayed earlier using ShowWindow</param>
        public void HideWindow(FrameworkElement windowContents)
        {
            if (windowContents == null)
                throw new ArgumentNullException("windowContents");

            int hashCode = windowContents.GetHashCode();
            ChildWindow childWindow = null;
            if (m_ChildWindows.TryGetValue(hashCode, out childWindow))
            {
                childWindow.Close();
                // close will automatically remove the window from the hash table in childWindow_Closed
            }             
        }