Пример #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>());
 }
Пример #2
0
 /// <summary>
 /// Find a nearby item within a parent item
 /// <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 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);
 }
Пример #3
0
        /// <summary>
        /// Setup for appropriate handling of Disposable View Models in WPF
        /// <para>If used on a Window, will attach to Closed and Dispatcher.ShutdownStarted events to dispose of ViewModel</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 ViewModel (DataContext)</param>
        public static void HandleDisposableViewModel(this FrameworkElement fe)
        {
            Action Dispose = () =>
            {
                var DataContext = fe.DataContext as IDisposable;
                if (DataContext != null)
                {
                    DataContext.Dispose();
                }
            };

            var win = fe as Window;

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

            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;
            }
        }