Exemplo n.º 1
0
        private static void ChangeOwner(Toastinet toast, FrameworkElement parent)
        {
            // Remove toast from parent
            if (parent != null)
            {
                IEnumerable <UIElement> parentContainer = VisualTreeHelperUtil.GetControlsDecendant <Grid>(parent);
                var grid = parentContainer.FirstOrDefault() as System.Windows.Controls.Grid;
                if (grid != null)
                {
                    grid.Children.Remove(toast);
                }
                else
                {
                    var panel = parentContainer.FirstOrDefault() as StackPanel;
                    if (panel != null)
                    {
                        panel.Children.Remove(toast);
                    }
                }
            }

            // Set ZIndex of the dynamic toast
            Canvas.SetZIndex(toast, 99999);

            // Try to find First Grid
            IEnumerable <UIElement> container = VisualTreeHelperUtil.GetControlsDecendant <Grid>(toast.Owner);

            if (container.FirstOrDefault() != null)
            {
                ((Grid)container.First()).Children.Insert(0, toast);
            }
            else
            {
                //no grid, is there a stackpanel
                container = VisualTreeHelperUtil.GetControlsDecendant <StackPanel>(toast.Owner);
                if (container.FirstOrDefault() != null)
                {
                    ((Grid)container.First()).Children.Insert(0, toast);
                }
                else
                {
                    throw new Exception("Unable to find window container of type Grid or StackPanel");
                }
            }

            VisualStateManager.GoToState(toast, toast.GetValidAnimation() + "Opened", true);
            VisualStateManager.GoToState(toast, toast.GetValidAnimation() + "Closed", true);
        }
Exemplo n.º 2
0
 public static FrameworkElement GetFocusedElement(DependencyObject reference)
 {
     return(VisualTreeHelperUtil.GetControlsDecendant <FrameworkElement>(reference).FirstOrDefault(fe => fe.IsFocused));
 }