Beispiel #1
0
 /// <summary>
 /// Find nearby items within a parent element
 /// <para>For example, a Button may have a Textbox nearby it in the same Grid, and this can find that</para>
 /// </summary>
 /// <typeparam name="T">Type of element to find nearby</typeparam>
 /// <typeparam name="TParentType">Type of the Parent element to search within</typeparam>
 /// <param name="fromEl">Element to find the other nearby element from</param>
 /// <returns>The nearby elements</returns>
 public static IEnumerable <T> FindAllNear <T, TParentType>(UIElement fromEl)
     where T : UIElement
     where TParentType : UIElement
 {
     if (fromEl != null)
     {
         var parent = UIElementHelper.FindParentOf <TParentType>(fromEl);
         if (parent != null)
         {
             return(UIElementHelper.GetChildren <T>(parent));
         }
     }
     return(Enumerable.Empty <T>());
 }
Beispiel #2
0
 /// <summary>
 /// Find a nearby item within a parent Grid
 /// <para>For example, a Button may have a Textbox nearby it in the same Grid, and this can find that</para>
 /// <para>Will always search for nearby within a Grid specifically</para>
 /// </summary>
 /// <typeparam name="T">Type of element to find nearby</typeparam>
 /// <typeparam name="TParentType">Type of the Parent element to search within</typeparam>
 /// <param name="fromEl">Element to find the other nearby element from</param>
 /// <returns>The nearby element, or null if none found</returns>
 public static T FindNear <T, TParentType>(UIElement fromEl)
     where T : UIElement
     where TParentType : UIElement
 {
     if (fromEl != null)
     {
         var grid = UIElementHelper.FindParentOf <TParentType>(fromEl);
         if (grid != null)
         {
             var near = UIElementHelper.GetChild <T>(grid);
             if (near != null)
             {
                 return(near);
             }
         }
     }
     return(null);
 }
Beispiel #3
0
        /// <summary>
        /// Setup for appropriate handling of IDisposable objects in WPF Windows
        /// <para>If used on a Window, will attach to Closed and Dispatcher.ShutdownStarted events to dispose of object</para>
        /// <para>If used on other UIElement, will search for parent Window in the Visual Tree and attach to that window's events</para>
        /// <para>If used on any other object, nothing will happen</para>
        /// </summary>
        /// <param name="fe">FrameworkElement object to attach to for Disposing of object</param>
        /// <param name="disposable">Disposable object to dispose of when appropriate</param>
        public static void HandleDisposable(this FrameworkElement fe, IDisposable disposable)
        {
            Action Dispose = () =>
            {
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            };

            var win = fe as Window;

            if (win == null)
            {
                var uiEl = fe as UIElement;
                if (uiEl != null)
                {
                    win = UIElementHelper.FindParentOf <Window>(uiEl);
                }
            }

            if (win != null)
            {
                EventHandler WinClosed       = null;
                EventHandler ShutdownStarted = null;

                WinClosed = (sender, e) =>
                {
                    Dispose();
                    win.Dispatcher.ShutdownStarted -= ShutdownStarted;
                    win.Closed -= WinClosed;
                };
                ShutdownStarted = (sender, e) =>
                {
                    Dispose();
                    win.Closed -= WinClosed;
                    win.Dispatcher.ShutdownStarted -= ShutdownStarted;
                };

                win.Closed += WinClosed;
                win.Dispatcher.ShutdownStarted += ShutdownStarted;
            }
        }