Exemple #1
0
        public static string GeneratePathString(DependencyObject element, string keyPrefix, string keySuffix)
        {
            // get a listing of the types from SketchFlow Player's model
            List <string> screenTypeNames = (
                from screen in PlayerContext.Instance.RuntimeData.Screens
                select screen.ClassName)
                                            .ToList();

            // find the topmost SketchFlow screen
            DependencyObject topmostScreen = (
                from ancestor in DependencyObjectHelper.GetSelfAndVisualAncestors(element)
                let typeName = ancestor.GetType().FullName
                               where screenTypeNames.Contains(typeName)
                               select ancestor)
                                             .LastOrDefault();

            if (topmostScreen == null)
            {
                return(null);
            }

            // get the visual ancestors up to but not including the topmost screen
            List <DependencyObject> visualHierarchy = DependencyObjectHelper.GetSelfAndVisualAncestors(element)
                                                      .TakeWhile(visual => visual != topmostScreen)
                                                      .ToList();

            // walk up to it, recording child indices at each level in the visual tree
            IEnumerable <int> indices = (
                from child in visualHierarchy
                let parent = VisualTreeHelper.GetParent(child)
                             let siblings = DependencyObjectHelper.GetVisualChildren(parent).ToList()
                                            let index = siblings.IndexOf(child)
                                                        select index)
                                        .Reverse();

            // create the path to the screen
            string key = string.Format(PathFormatString, keyPrefix, topmostScreen.GetType().FullName);

            foreach (int index in indices)
            {
                key = string.Format(PathFormatString, key, index);
            }

            // add the group name
            key = string.Format(PathFormatString, key, keySuffix);

            return(key);
        }
        private void PreserveStateVisitor(Action <FrameworkElement, DependencyProperty, string> preserveStateAction, DependencyObject parent)
        {
            FrameworkElement parentElement = parent as FrameworkElement;

            if (parentElement != null)
            {
                foreach (PreservePropertyInfo target in propertiesToPreserve
                         .Where(target => target.Type.IsAssignableFrom(parent.GetType())))
                {
                    preserveStateAction(parentElement, target.Property, target.PropertyName);
                }
            }

            foreach (DependencyObject child in DependencyObjectHelper.GetVisualChildren(parent))
            {
                PreserveStateVisitor(preserveStateAction, child);
            }
        }