Exemplo n.º 1
0
        /// <inheritdoc/>
        protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
        {
            if (oldView != null)
            {
                if (IsDefault)
                {
                    oldView.UnregisterDefaultButton(this);
                }

                if (IsCancel)
                {
                    oldView.UnregisterCancelButton(this);
                }
            }

            if (newView != null)
            {
                if (IsDefault)
                {
                    newView.RegisterDefaultButton(this);
                }

                if (IsCancel)
                {
                    newView.RegisterCancelButton(this);
                }
            }

            UpdateIsDefaulted();

            base.OnViewChanged(oldView, newView);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to perform navigation as a result of the specified game pad button press.
        /// </summary>
        /// <param name="view">The view for which to perform navigation.</param>
        /// <param name="device">The game pad device that raised the button press event.</param>
        /// <param name="button">The button that was pressed.</param>
        /// <returns><c>true</c> if navigation was performed; otherwise, <c>false</c>.</returns>
        public static Boolean PerformNavigation(PresentationFoundationView view, GamePadDevice device, GamePadButton button)
        {
            Contract.Require(view, "view");

            var element = (view.ElementWithFocus ?? view.LayoutRoot) as UIElement;
            if (element == null)
                return false;

            if (GamePad.TabButton == button)
                return PerformNavigation(view, element, FocusNavigationDirection.Next, false);

            if (GamePad.ShiftTabButton == button)
                return PerformNavigation(view, element, FocusNavigationDirection.Previous, false);

            if (!GamePad.UseAxisForDirectionalNavigation)
            {
                switch (button)
                {
                    case GamePadButton.DPadUp:
                        return PerformNavigation(view, element, FocusNavigationDirection.Up, false);

                    case GamePadButton.DPadDown:
                        return PerformNavigation(view, element, FocusNavigationDirection.Down, false);

                    case GamePadButton.DPadLeft:
                        return PerformNavigation(view, element, FocusNavigationDirection.Left, false);

                    case GamePadButton.DPadRight:
                        return PerformNavigation(view, element, FocusNavigationDirection.Right, false);
                }
            }

            return false;
        }
        /// <summary>
        /// Executes the command.
        /// </summary>
        /// <param name="view">The view within which the command is being executed.</param>
        /// <param name="parameter">The command parameter, or <see langword="null"/> if the command
        /// does not require a parameter.</param>
        /// <param name="valparameter">The command value parameter, or <see langword="null"/> if the command
        /// does not require a value parameter. This parameter is intended for use in internal micro-optimizations and
        /// will appear as one of the fields in the routed event data.</param>
        /// <param name="target">The element within <paramref name="view"/> at which to begin
        /// searching for command handlers.</param>
        public void Execute(PresentationFoundationView view, Object parameter, PrimitiveUnion?valparameter, IInputElement target)
        {
            Contract.Require(view, nameof(view));

            var uiElement = target as UIElement;

            if (uiElement != null && uiElement.View != view)
            {
                throw new ArgumentException(PresentationStrings.ElementDoesNotBelongToView);
            }

            if (uiElement != null)
            {
                var data = ExecutedRoutedEventData.Retrieve(this, valparameter, autorelease: false);
                try
                {
                    var evtPreview = EventManager.GetInvocationDelegate <UpfExecutedRoutedEventHandler>(CommandManager.PreviewExecutedEvent);
                    evtPreview(uiElement, this, parameter, data);

                    if (!data.Handled)
                    {
                        var evt = EventManager.GetInvocationDelegate <UpfExecutedRoutedEventHandler>(CommandManager.ExecutedEvent);
                        evt(uiElement, this, parameter, data);
                    }
                }
                finally
                {
                    data.Release();
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Attempts to perform navigation as a result of the specified key press.
        /// </summary>
        /// <param name="view">The view for which to perform navigation.</param>
        /// <param name="device">The keyboard device that raised the key press event.</param>
        /// <param name="key">The key that was pressed.</param>
        /// <param name="ctrl">A value indicating whether the Ctrl modifier is active.</param>
        /// <param name="alt">A value indicating whether the Alt modifier is active.</param>
        /// <param name="shift">A value indicating whether the Shift modifier is active.</param>
        /// <param name="repeat">A value indicating whether this is a repeated key press.</param>
        /// <returns><c>true</c> if navigation was performed; otherwise, <c>false</c>.</returns>
        public static Boolean PerformNavigation(PresentationFoundationView view, KeyboardDevice device, Key key, Boolean ctrl, Boolean alt, Boolean shift, Boolean repeat)
        {
            Contract.Require(view, "view");

            var element = (view.ElementWithFocus ?? view.LayoutRoot) as UIElement;
            if (element == null)
                return false;

            switch (key)
            {
                case Key.Tab:
                    return PerformNavigation(view, element, shift ? FocusNavigationDirection.Previous : FocusNavigationDirection.Next, ctrl);

                case Key.Left:
                    return PerformNavigation(view, element, FocusNavigationDirection.Left, false);

                case Key.Up:
                    return PerformNavigation(view, element, FocusNavigationDirection.Up, false);

                case Key.Right:
                    return PerformNavigation(view, element, FocusNavigationDirection.Right, false);

                case Key.Down:
                    return PerformNavigation(view, element, FocusNavigationDirection.Down, false);
            }

            return false;
        }
Exemplo n.º 5
0
 /// <inheritdoc/>
 protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
 {
     if (newView != null)
     {
         UpdateTextParserCache();
     }
     base.OnViewChanged(oldView, newView);
 }
Exemplo n.º 6
0
 /// <inheritdoc/>
 protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
 {
     if (newView != null)
     {
         RebuildSelection();
     }
     base.OnViewChanged(oldView, newView);
 }
        /// <summary>
        /// Removes all elements associated with the specified view from the Foundation's processing queues.
        /// </summary>
        /// <param name="view">The view to remove from the queues.</param>
        internal void RemoveFromQueues(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            StyleQueue.Remove(view);
            MeasureQueue.Remove(view);
            ArrangeQueue.Remove(view);
        }
Exemplo n.º 8
0
        /// <inheritdoc/>
        protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
        {
            if (newView != null)
            {
                securePassword = new SecureString();
            }

            base.OnViewChanged(oldView, newView);
        }
        /// <summary>
        /// Creates a new instance <see cref="DataSourceDefinition"/> for the specified component template.
        /// </summary>
        /// <param name="templatedControl">The type of control to which the template is applied.</param>
        /// <param name="definition">The XML element that defines the component template.</param>
        /// <returns>The <see cref="DataSourceDefinition"/> that was created.</returns>
        public static DataSourceDefinition FromComponentTemplate(Type templatedControl, XElement definition)
        {
            var dataSourceIdentifier       = templatedControl.Name;
            var dataSourceWrapperName      = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(templatedControl);
            var dataSourceWrapperNamespace = PresentationFoundationView.DataSourceWrapperNamespaceForComponentTemplates;

            return(new DataSourceDefinition(templatedControl.FullName,
                                            dataSourceIdentifier, dataSourceWrapperName, dataSourceWrapperNamespace, templatedControl, definition));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CursorTrackerTouchCollection"/> class.
        /// </summary>
        /// <param name="view"></param>
        public CursorTrackerTouchCollection(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            pool = new ExpandingPool <CursorTracker.Touch>(5,
                                                           () => CursorTracker.ForTouch(view),
                                                           (tracker) => tracker.OnReleaseIntoPool());
            active = new Dictionary <Int64, CursorTracker.Touch>(5);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Removes all elements associated with the specified view from the queue.
        /// </summary>
        /// <param name="view">The view to remove from the queue.</param>
        public void Remove(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            var keys = queue.Keys.Where(x => x.Element?.View == view).ToList();

            foreach (var key in keys)
            {
                queue.Remove(key);
            }
        }
        /// <summary>
        /// Compiles a delegate which invokes a method on the view model.
        /// </summary>
        private static TLambda CompileViewModelMethodLambda <TLambda>(PresentationFoundationView view, MethodInfo methodInfo)
        {
            var invoke       = typeof(TLambda).GetMethod("Invoke");
            var invokeParams = invoke.GetParameters();

            var expParams         = invokeParams.Select(x => Expression.Parameter(x.ParameterType, x.Name)).ToList();
            var expView           = Expression.Constant(view, typeof(PresentationFoundationView));
            var expViewModel      = Expression.Property(expView, nameof(PresentationFoundationView.ViewModel));
            var expViewModelTyped = Expression.Convert(expViewModel, view.ViewModelType);
            var expInvoke         = Expression.Call(expViewModelTyped, methodInfo, expParams);

            return(Expression.Lambda <TLambda>(expInvoke, expParams).Compile());
        }
Exemplo n.º 13
0
        /// <summary>
        /// Captures the mouse within the specified input element.
        /// </summary>
        /// <param name="view">The view for which to set mouse capture.</param>
        /// <param name="element">The element to capture the mouse.</param>
        /// <param name="mode">The mouse capture mode.</param>
        /// <returns><c>true</c> if the mouse was successfully captured; otherwise, <c>false</c>.</returns>
        public static Boolean Capture(PresentationFoundationView view, IInputElement element, CaptureMode mode)
        {
            Contract.Require(view, "view");

            if (element != null)
            {
                return view.CaptureMouse(element, mode);
            }
            else
            {
                view.ReleaseMouse();
                return true;
            }
        }
Exemplo n.º 14
0
        /// <inheritdoc/>
        protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
        {
            if (oldView == null && newView != null)
            {
                LayoutUpdated += ComboBox_LayoutUpdated;
            }

            if (oldView != null && newView == null)
            {
                LayoutUpdated -= ComboBox_LayoutUpdated;
            }

            base.OnViewChanged(oldView, newView);
        }
        /// <summary>
        /// Finds all of the compiled binding expressions on the current data
        /// source and adds them to the context's registry.
        /// </summary>
        private void FindCompiledBindingExpressions()
        {
            var wrapperName = default(String);
            var wrapperType = DataSource is PresentationFoundationView ? DataSourceType : null;

            if (wrapperType == null)
            {
                for (var templateType = TemplatedParent.GetType(); templateType != null; templateType = templateType.BaseType)
                {
                    wrapperName = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(templateType);
                    wrapperType = Ultraviolet.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(wrapperName);

                    if (wrapperType != null)
                    {
                        break;
                    }
                }

                if (wrapperType == null)
                {
                    wrapperName = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(TemplatedParent.GetType());
                    throw new UvmlException(PresentationStrings.CannotFindViewModelWrapper.Format(wrapperName));
                }
            }

            var properties = wrapperType.GetProperties().Where(x => x.Name.StartsWith("__UPF_Expression")).ToList();
            var propertiesWithExpressions = from prop in properties
                                            let attr                       = (CompiledBindingExpressionAttribute)prop.GetCustomAttributes(typeof(CompiledBindingExpressionAttribute), false).Single()
                                                                  let expr = attr.Expression
                                                                             select new
            {
                Property   = prop,
                Expression = expr,
            };

            foreach (var prop in propertiesWithExpressions)
            {
                var key = new CompiledBindingExpressionKey(prop.Property.PropertyType, prop.Expression);
                compiledBindingExpressions.Add(key, prop.Property);
            }

            var uniques = compiledBindingExpressions.GroupBy(x => x.Key).Where(x => x.Count() == 1).ToList();

            foreach (var unique in uniques)
            {
                var key = new CompiledBindingExpressionKey(null, unique.Key.Expression);
                compiledBindingExpressions[key] = unique.Single().Value;
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PresentationFoundationViewResources"/> class.
        /// </summary>
        /// <param name="view">The view that owns this resource collection.</param>
        internal PresentationFoundationViewResources(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            this.view = view;

            this.StringFormatter = new StringFormatter();
            this.StringBuffer    = new StringBuilder();
            this.TextRenderer    = new TextRenderer();

            if (view.TextShaper != null)
            {
                this.TextRenderer.RegisterTextShaper(view.TextShaper);
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// Performs initialization required by instances of the <see cref="ContentPresenter"/> class.
        /// </summary>
        private void InitializeContentPresenter(UltravioletContext uv, Object instance, UvmlInstantiationContext context)
        {
            var contentPresenter = instance as ContentPresenter;

            if (contentPresenter == null)
            {
                return;
            }

            if (contentPresenter.HasDefinedValue(ContentPresenter.ContentProperty) || contentPresenter.TemplatedParent == null)
            {
                return;
            }

            var alias = contentPresenter.ContentSource ?? "Content";

            if (alias == String.Empty)
            {
                return;
            }

            var templateType        = contentPresenter.TemplatedParent.GetType();
            var templateWrapperName = PresentationFoundationView.GetDataSourceWrapperNameForComponentTemplate(templateType);
            var templateWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(templateWrapperName);

            var dpAliasedContent = DependencyProperty.FindByName(alias, templateType);

            if (dpAliasedContent != null)
            {
                contentPresenter.BindValue(ContentPresenter.ContentProperty, templateWrapperType,
                                           "{{" + dpAliasedContent.Name + "}}");
            }

            if (!contentPresenter.HasDefinedValue(ContentPresenter.ContentStringFormatProperty))
            {
                var dpAliasedContentStringFormat = DependencyProperty.FindByName(alias + "StringFormat", templateType);
                if (dpAliasedContentStringFormat != null)
                {
                    contentPresenter.BindValue(ContentPresenter.ContentStringFormatProperty, templateWrapperType,
                                               "{{" + dpAliasedContentStringFormat.Name + "}}");
                }
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Prepares for keyboard navigation and potentially changes the current element and direction.
        /// </summary>
        /// <param name="view">The view for which to perform navigation.</param>
        /// <param name="element">The element at which to begin navigation.</param>
        /// <param name="direction">The direction in which to navigate focus.</param>
        /// <returns><c>true</c> if navigation can be performed; otherwise, false.</returns>
        private static Boolean PrepareNavigation(PresentationFoundationView view, ref UIElement element, ref FocusNavigationDirection direction)
        {
            if (element != null)
                return true;

            if (IsDirectionalNavigation(direction))
                return false;

            element = view.LayoutRoot;
            return true;
        }
Exemplo n.º 19
0
        /// <inheritdoc/>
        protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
        {
            UpdateTextParserResult();

            base.OnViewChanged(oldView, newView);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Creates a new <see cref="CursorTracker"/> which tracks the mouse cursor.
        /// </summary>
        /// <param name="view">The view for which to create the tracker.</param>
        /// <returns>The <see cref="CursorTracker"/> instance which was created.</returns>
        public static Mouse ForMouse(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            return(new Mouse(view, Input.Mouse.PrimaryDevice));
        }
Exemplo n.º 21
0
        /// <summary>
        /// Attempts to navigate focus in the specified direction.
        /// </summary>
        /// <param name="view">The view for which to perform navigation.</param>
        /// <param name="element">The element at which to begin navigation.</param>
        /// <param name="direction">The direction in which to navigate focus.</param>
        /// <param name="ctrl">A value indicating whether the Ctrl modifier is active.</param>
        /// <returns><c>true</c> if navigation was performed; otherwise, <c>false</c>.</returns>
        public static Boolean PerformNavigation(PresentationFoundationView view, UIElement element, FocusNavigationDirection direction, Boolean ctrl)
        {
            if (!PrepareNavigation(view, ref element, ref direction))
                return false;

            var navprop      = GetNavigationProperty(direction, ctrl);
            var navContainer = default(DependencyObject);
            var destination  = default(IInputElement);
            
            switch (direction)
            {
                case FocusNavigationDirection.Next:
                    navContainer = FindNavigationContainer(element, navprop);
                    destination  = FindNextNavigationStop(view, navContainer, element, navprop, false) as IInputElement;
                    break;

                case FocusNavigationDirection.Previous:
                    navContainer = FindNavigationContainer(element, navprop);
                    destination  = FindPrevNavigationStop(view, navContainer, element, navprop, false) as IInputElement;
                    break;

                case FocusNavigationDirection.First:
                    destination = FindNextNavigationStop(view, element, null, navprop, true) as IInputElement;
                    break;

                case FocusNavigationDirection.Last:
                    destination = FindPrevNavigationStop(view, element, null, navprop, true) as IInputElement;
                    break;

                case FocusNavigationDirection.Left:
                case FocusNavigationDirection.Right:
                case FocusNavigationDirection.Up:
                case FocusNavigationDirection.Down:
                    navContainer = FindNavigationContainer(element, navprop, false);
                    if (navContainer != null)
                    {
                        destination = FindNavigationStopInDirection(view, navContainer, element, navprop, direction) as IInputElement;
                    }
                    break;
            }            

            if (destination != null)
            {
                view.FocusElement(destination);
                return true;
            }
            return false;
        }
Exemplo n.º 22
0
 /// <inheritdoc/>
 protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
 {
     if (newView != null)
     {
         RebuildSelection();
     }
     base.OnViewChanged(oldView, newView);
 }
Exemplo n.º 23
0
 /// <inheritdoc/>
 protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
 {
     UpdateCanExecute();
     base.OnViewChanged(oldView, newView);
 }
Exemplo n.º 24
0
        /// <inheritdoc/>
        protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
        {
            UpdateTextParserResult();

            base.OnViewChanged(oldView, newView);
        }
Exemplo n.º 25
0
        /// <summary>
        /// Finds the next navigation stop in the specified direction.
        /// </summary>
        /// <param name="view">The view for which navigation is being performed.</param>
        /// <param name="navContainer">The navigation container that contains <paramref name="navElement"/>.</param>
        /// <param name="navElement">The element at which to begin the search.</param>
        /// <param name="navProp">The navigation property to evaluate.</param>
        /// <param name="direction">The direction in which to navigate.</param>
        /// <returns>The next navigation stop in the specified direction, or <c>null</c>.</returns>
        private static DependencyObject FindNavigationStopInDirection(PresentationFoundationView view, DependencyObject navContainer, DependencyObject navElement, DependencyProperty navProp, FocusNavigationDirection direction)
        {
            if (navContainer != null && navContainer.GetValue<KeyboardNavigationMode>(navProp) == KeyboardNavigationMode.Once)
            {
                navElement = navContainer;
                navContainer = FindNavigationContainer(navElement, navProp, false);
            }

            var uiElement = navElement as UIElement;
            if (uiElement == null)
                return null;

            return FindNavigationStopInDirection(view, navContainer, uiElement.TransformedVisualBounds, navProp, direction);
        }
Exemplo n.º 26
0
 /// <summary>
 /// Determines whether the command can be executed.
 /// </summary>
 /// <param name="view">The view within which the command is being executed.</param>
 /// <returns><see langword="true"/> if the command can be executed; otherwise, <see langword="false"/>.</returns>
 public void Execute(PresentationFoundationView view) => Execute(view, null);
Exemplo n.º 27
0
 /// <summary>
 /// Executes the command.
 /// </summary>
 /// <param name="view">The view within which the command is being executed.</param>
 public bool CanExecute(PresentationFoundationView view) => CanExecute(view, null);
Exemplo n.º 28
0
        /// <summary>
        /// Attempts to perform navigation as a result of the specified game pad axis press.
        /// </summary>
        /// <param name="view">The view for which to perform navigation.</param>
        /// <param name="device">The game pad device that raised the button press event.</param>
        /// <param name="axis">The axis that was pressed.</param>
        /// <returns><c>true</c> if navigation was performed; otherwise, <c>false</c>.</returns>
        public static Boolean PerformNavigation(PresentationFoundationView view, GamePadDevice device, GamePadAxis axis)
        {
            Contract.Require(view, "view");

            if (GamePad.UseAxisForDirectionalNavigation)
            {
                var element = (view.ElementWithFocus ?? view.LayoutRoot) as UIElement;
                if (element == null)
                    return false;

                if (GamePad.DirectionalNavigationAxisX == axis || GamePad.DirectionalNavigationAxisY == axis)
                {
                    var direction = device.GetJoystickDirectionFromAxis(axis);
                    var succeeded = false;

                    if ((direction & GamePadJoystickDirection.Up) == GamePadJoystickDirection.Up)
                        succeeded = succeeded || PerformNavigation(view, element, FocusNavigationDirection.Up, false);

                    if ((direction & GamePadJoystickDirection.Down) == GamePadJoystickDirection.Down)
                        succeeded = succeeded || PerformNavigation(view, element, FocusNavigationDirection.Down, false);

                    if ((direction & GamePadJoystickDirection.Left) == GamePadJoystickDirection.Left)
                        succeeded = succeeded || PerformNavigation(view, element, FocusNavigationDirection.Left, false);

                    if ((direction & GamePadJoystickDirection.Right) == GamePadJoystickDirection.Right)
                        succeeded = succeeded || PerformNavigation(view, element, FocusNavigationDirection.Right, false);
                }
            }
            return false;
        }
Exemplo n.º 29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Touch"/> class.
 /// </summary>
 /// <param name="view">The view which owns this tracker.</param>
 /// <param name="device">The touch device which is being tracked.</param>
 public Touch(PresentationFoundationView view, TouchDevice device)
     : base(view)
 {
     this.device = device;
 }
Exemplo n.º 30
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Mouse"/> class.
 /// </summary>
 /// <param name="view">The view which owns this tracker.</param>
 /// <param name="device">The mouse device which is being tracked.</param>
 public Mouse(PresentationFoundationView view, MouseDevice device)
     : base(view)
 {
     this.device = device;
 }
Exemplo n.º 31
0
        /// <inheritdoc/>
        protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
        {
            if (oldView != null)
            {
                if (IsDefault)
                    oldView.UnregisterDefaultButton(this);

                if (IsCancel)
                    oldView.UnregisterCancelButton(this);
            }

            if (newView != null)
            {
                if (IsDefault)
                    newView.RegisterDefaultButton(this);

                if (IsCancel)
                    newView.RegisterCancelButton(this);
            }

            UpdateIsDefaulted();

            base.OnViewChanged(oldView, newView);
        }
Exemplo n.º 32
0
        /// <inheritdoc/>
        protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
        {
            if (newView != null)
                securePassword = new SecureString();

            base.OnViewChanged(oldView, newView);
        }
Exemplo n.º 33
0
        /// <inheritdoc/>
        protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
        {
            if (oldView == null && newView != null)
            {
                LayoutUpdated += CheckAdornersForChangedOnLayoutUpdated;
            }

            if (oldView != null && newView == null)
            {
                LayoutUpdated -= CheckAdornersForChangedOnLayoutUpdated;
            }

            base.OnViewChanged(oldView, newView);
        }
Exemplo n.º 34
0
        /// <summary>
        /// Gets the element which has captured the mouse within the specified view.
        /// </summary>
        /// <param name="view">The view to evaluate.</param>
        /// <returns>The element which has captured the mouse within the specified view, 
        /// or <see langword="null"/> if no element has captured the mouse.</returns>
        public static IInputElement GetCaptured(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            return view.ElementWithMouseCapture;
        }
Exemplo n.º 35
0
        /// <summary>
        /// Searches the visual tree for the previous element in the tab order before <paramref name="navElement"/>.
        /// </summary>
        /// <param name="view">The view for which navigation is being performed.</param>
        /// <param name="navContainer">The navigation container that contains <paramref name="navElement"/>.</param>
        /// <param name="navElement">The element at which to begin the search.</param>
        /// <param name="navProp">The navigation property to evaluate.</param>
        /// <param name="local">A value indicating whether the search is restricted only to the visual subtree of <paramref name="navContainer"/>.</param>
        /// <returns>The previous element in the tab order before <paramref name="navElement"/>, or <c>null</c>.</returns>
        private static DependencyObject FindPrevNavigationStop(PresentationFoundationView view, DependencyObject navContainer, DependencyObject navElement, DependencyProperty navProp, Boolean local)
        {
            var navMode = navContainer.GetValue<KeyboardNavigationMode>(navProp);

            if (navElement == null)
            {
                var selectedTab = GetSelectedTab(navContainer as UIElement);
                if (selectedTab != null)
                    return FindPrevNavigationStop(view, selectedTab, null, navProp, local);
            }

            if (navElement != null && (navMode == KeyboardNavigationMode.Once || navMode == KeyboardNavigationMode.None))
            {
                if (local || navContainer == navElement)
                    return null;

                if (IsNavigationStop(navContainer))
                    return navContainer;

                var navContainerContainer = FindNavigationContainer(navContainer, navProp, false);
                return FindPrevNavigationStop(view, navContainerContainer, navContainer, navProp, false);
            }

            if (navElement == null && (navMode == KeyboardNavigationMode.Once))
            {
                var descendant = FindNextVisualElementWithinContainer(navContainer, null, navProp, navMode);
                if (descendant != null)
                {
                    return FindPrevNavigationStop(view, descendant, null, navProp, true);
                }
                else
                {
                    if (IsNavigationStop(navContainer))
                        return navContainer;

                    if (local)
                        return null;
                    
                    var navContainerContainer = FindNavigationContainer(navContainer, navProp, false);
                    return FindPrevNavigationStop(view, navContainerContainer, navContainer, navProp, false);
                }
            }

            var first = default(DependencyObject);
            var prev = navElement;

            while (true)
            {
                prev = FindPrevVisualElementWithinContainer(navContainer, prev, navProp, navMode);
                if (prev == null || (prev == navContainer && (navMode == KeyboardNavigationMode.Local || navMode == KeyboardNavigationMode.Contained)))
                    break;

                if (IsNavigationStop(prev) && !IsNavigationContainer(prev, navProp))
                    return prev;

                if (first == prev)
                    break;

                if (first == null)
                    first = prev;

                var last = FindPrevNavigationStop(view, prev, null, navProp, true);
                if (last != null)
                    return last;
            }

            if (navMode != KeyboardNavigationMode.Contained)
            {
                if (navElement != navContainer && IsNavigationStop(navContainer))
                    return navContainer;

                if (local)
                    return null;

                if (VisualTreeHelper.GetParent(navContainer) != null)
                {
                    var navContainerContainer = FindNavigationContainer(navContainer, navProp, false);
                    return FindPrevNavigationStop(view, navContainerContainer, navContainer, navProp, false);
                }

                return FindPrevNavigationStop(view, navContainer, null, navProp, true);
            }

            return null;
        }
Exemplo n.º 36
0
        /// <summary>
        /// Finds the next navigation stop in the specified direction.
        /// </summary>
        /// <param name="view">The view for which navigation is being performed.</param>
        /// <param name="navContainer">The navigation container within which to search.</param>
        /// <param name="navElementBounds">The bounding box of the element at which to begin the search.</param>
        /// <param name="navProp">The navigation property to evaluate.</param>
        /// <param name="direction">The direction in which to navigate.</param>
        /// <param name="local">A value indicating whether the search is restricted only to the visual subtree of <paramref name="navContainer"/>.</param>
        /// <returns>The next navigation stop in the specified direction, or <c>null</c>.</returns>
        private static DependencyObject FindNavigationStopInDirection(PresentationFoundationView view, DependencyObject navContainer, RectangleD navElementBounds, DependencyProperty navProp, FocusNavigationDirection direction, Boolean local = false)
        {
            var bestMatch = default(DependencyObject);
            var bestMatchScore = Int32.MaxValue;

            var current = VisualTreeHelper.GetFirstChild<UIElement>(navContainer, x => x.IsVisible);

            while (current != null)
            {
                if (IsInDirection(navElementBounds, current, direction) && (IsNavigationStop(current) || IsNavigationContainer(current, navProp)))
                {
                    if (!IsNavigationContainer(current, navProp) || current.GetValue<KeyboardNavigationMode>(navProp) != KeyboardNavigationMode.None)
                    {
                        var score = EvaluateDistanceScore(navElementBounds, current, direction);
                        if (score < bestMatchScore)
                        {
                            bestMatch = current;
                            bestMatchScore = score;
                        }
                        else
                        {
                            if (score == bestMatchScore && IsBetterDirectionalMatch(navElementBounds, bestMatch, current))
                            {
                                bestMatch = current;
                            }
                        }
                    }
                }
                current = TraverseVisualTreeNext(navContainer, current, navProp);
            }

            if (bestMatch == null)
            {
                var navMode = navContainer.GetValue<KeyboardNavigationMode>(navProp);
                switch (navMode)
                {
                    case KeyboardNavigationMode.Cycle:
                        if (!MoveBoundsOutsideOfContainer(ref navElementBounds, navContainer, direction))
                        {
                            return null;
                        }
                        return FindNavigationStopInDirection(view, navContainer, navElementBounds, navProp, direction);

                    case KeyboardNavigationMode.Contained:
                        return null;

                    default:
                        if (!local)
                        {
                            var navContainerContainer = FindNavigationContainer(navContainer, navProp, false);
                            if (navContainerContainer == null)
                            {
                                return null;
                            }
                            return FindNavigationStopInDirection(view, navContainerContainer, navElementBounds, navProp, direction);
                        }
                        return null;
                }
            }
            else
            {
                if (IsNavigationContainer(bestMatch, navProp))
                {
                    bestMatch = FindNavigationStopInDirection(view, bestMatch, navElementBounds, navProp, direction, true) ?? bestMatch;
                }
            }

            return IsNavigationStop(bestMatch) ? bestMatch : null;
        }
 /// <summary>
 /// Creates an instantiation context for a view.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 /// <param name="view">The view which is being loaded.</param>
 /// <param name="viewModelType">The view model type for the view which is being loaded.</param>
 /// <returns>The instantiation context for the specified view.</returns>
 public static InstantiationContext FromView(UltravioletContext uv, PresentationFoundationView view, Type viewModelType)
 {
     return new InstantiationContext(uv, view, viewModelType);
 }
Exemplo n.º 38
0
        /// <summary>
        /// Loads an instance of the <see cref="PresentationFoundationView"/> from an XML node.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="uiPanel">The <see cref="UIPanel"/> that is creating the panel.</param>
        /// <param name="uiPanelDefinition">The <see cref="UIPanelDefinition"/> that defines the view.</param>
        /// <param name="vmfactory">A view model factory which is used to create the view's initial view model, or <see langword="null"/> to skip view model creation.</param>
        /// <returns>The <see cref="PresentationFoundationView"/> that was loaded from the specified XML element.</returns>
        public static PresentationFoundationView Load(UltravioletContext uv, UIPanel uiPanel, UIPanelDefinition uiPanelDefinition, UIViewModelFactory vmfactory)
        {
            Contract.Require(uv, nameof(uv));
            Contract.Require(uiPanel, nameof(uiPanel));
            Contract.Require(uiPanelDefinition, nameof(uiPanelDefinition));

            var viewElement = uiPanelDefinition.ViewElement;

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

            // Determine which culture to use when parsing values.
            var cultureRequested = (String)uiPanelDefinition.RootElement.Attribute("Culture");
            var cultureInfo      = CultureInfo.GetCultureInfo(cultureRequested ?? String.Empty);

            // Determine the type of view model used by this view.
            var viewModelType     = default(Type);
            var viewModelTypeName = (String)viewElement.Attribute("ViewModelType");

            if (viewModelTypeName != null)
            {
                viewModelType = Type.GetType(viewModelTypeName, false);

                if (viewModelType == null)
                {
                    throw new UvmlException(PresentationStrings.ViewModelTypeNotFound.Format(viewModelTypeName));
                }

                var viewModelWrapperAttr = viewModelType.GetCustomAttributes(typeof(ViewModelWrapperAttribute), false)
                                           .Cast <ViewModelWrapperAttribute>().SingleOrDefault();
                if (viewModelWrapperAttr != null)
                {
                    viewModelType = viewModelWrapperAttr.WrapperType;
                }
                else
                {
                    var viewModelWrapperName = PresentationFoundationView.GetDataSourceWrapperNameForView(uiPanelDefinition.AssetFilePath);
                    var viewModelWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(viewModelWrapperName);
                    if (viewModelWrapperType == null)
                    {
                        throw new UvmlException(PresentationStrings.CannotFindViewModelWrapper.Format(viewModelWrapperName));
                    }

                    viewModelType = viewModelWrapperType;
                }
            }

            // Create a UVML template which will instantiate the view.
            var viewTemplate = new UvmlTemplate(viewElement, typeof(PresentationFoundationView), (puv, pname) =>
            {
                var view      = new PresentationFoundationView(puv, uiPanel, viewModelType);
                var viewModel = vmfactory == null ? null : vmfactory(view);
                if (viewModel != null)
                {
                    view.SetViewModel(viewModel);
                }

                var root = view.LayoutRoot;
                root.BeginInit();

                var rootAdornerDecorator = new AdornerDecorator(puv, null);
                rootAdornerDecorator.BeginInit();
                root.Child = rootAdornerDecorator;

                var rootGridTemplate = CreateTemplateFromXml(puv, viewElement, null, typeof(Grid), cultureInfo);
                var rootGridContext  = UvmlInstantiationContext.ForView(puv, view);

                var rootGridTemplateInstance = (UvmlTemplateInstance)rootGridTemplate.Instantiate(puv, rootGridContext);
                var rootGrid = (Grid)rootGridTemplateInstance.Finalize();

                rootAdornerDecorator.Child = rootGrid;

                return(view);
            });

            // Instantiate the view template.
            var viewTemplateInstance = (UvmlTemplateInstance)viewTemplate.Instantiate(uv, null);
            var viewInstance         = (PresentationFoundationView)viewTemplateInstance.Finalize();

            var viewRoot = viewInstance.LayoutRoot;
            var viewRootAdornerDecorator = (AdornerDecorator)viewRoot.Child;

            viewRootAdornerDecorator.EndInit();
            viewRoot.EndInit();
            viewRoot.CacheLayoutParameters();

            var viewInstanceViewModel = viewInstance.ViewModel;

            if (viewInstanceViewModel != null)
            {
                viewInstance.Namescope.PopulateFieldsFromRegisteredElements(viewInstanceViewModel);
            }

            return(viewInstance);
        }
Exemplo n.º 39
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CursorTracker"/> class.
 /// </summary>
 /// <param name="view">The view which owns this tracker.</param>
 protected CursorTracker(PresentationFoundationView view)
 {
     this.View = view;
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="PresentationFoundationViewResources"/> class.
        /// </summary>
        /// <param name="view">The view that owns this resource collection.</param>
        internal PresentationFoundationViewResources(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            this.view = view;
        }
Exemplo n.º 41
0
        /// <summary>
        /// Creates a new <see cref="CursorTracker"/> which tracks touches.
        /// </summary>
        /// <param name="view">The view for which to create the tracker.</param>
        /// <returns>The <see cref="CursorTracker"/> instance which was created.</returns>
        public static Touch ForTouch(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            return(new Touch(view, Input.Touch.PrimaryDevice));
        }
Exemplo n.º 42
0
        /// <summary>
        /// Gets the element which has keyboard focus within the specified view.
        /// </summary>
        /// <param name="view">The view to evaluate.</param>
        /// <returns>The element which has keyboard focus within the specified view, or <see langword="null"/>
        /// if no element currently has focus.</returns>
        public static IInputElement GetFocusedElement(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            return(view.ElementWithFocus);
        }
Exemplo n.º 43
0
 /// <summary>
 /// Creates an instantiation context for the specified view.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 /// <param name="view">The view for which to create an instantiation context.</param>
 /// <returns>The instantiation context which was created.</returns>
 internal static UvmlInstantiationContext ForView(UltravioletContext uv, PresentationFoundationView view)
 {
     return(new UvmlInstantiationContext(uv, null, view, view.ViewModelType, view.Namescope));
 }
Exemplo n.º 44
0
        /// <summary>
        /// Gets the element which has keyboard focus within the specified view.
        /// </summary>
        /// <param name="view">The view to evaluate.</param>
        /// <returns>The element which has keyboard focus within the specified view, or <c>null</c>
        /// if no element currently has focus.</returns>
        public static IInputElement GetFocusedElement(PresentationFoundationView view)
        {
            Contract.Require(view, "view");

            return view.ElementWithFocus;
        }
Exemplo n.º 45
0
        /// <summary>
        /// Loads an instance of the <see cref="PresentationFoundationView"/> from an XML node.
        /// </summary>
        /// <param name="uv">The Ultraviolet context.</param>
        /// <param name="uiPanel">The <see cref="UIPanel"/> that is creating the panel.</param>
        /// <param name="uiPanelDefinition">The <see cref="UIPanelDefinition"/> that defines the view.</param>
        /// <param name="vmfactory">A view model factory which is used to create the view's initial view model, or <see langword="null"/> to skip view model creation.</param>
        /// <returns>The <see cref="PresentationFoundationView"/> that was loaded from the specified XML element.</returns>
        public static PresentationFoundationView Load(UltravioletContext uv, UIPanel uiPanel, UIPanelDefinition uiPanelDefinition, UIViewModelFactory vmfactory)
        {
            Contract.Require(uv, nameof(uv));
            Contract.Require(uiPanel, nameof(uiPanel));
            Contract.Require(uiPanelDefinition, nameof(uiPanelDefinition));

            var viewElement = uiPanelDefinition.ViewElement;
            if (viewElement == null)
                return null;

            // Determine which culture to use when parsing values.
            var cultureRequested = (String)uiPanelDefinition.RootElement.Attribute("Culture");
            var cultureInfo = CultureInfo.GetCultureInfo(cultureRequested ?? String.Empty);

            // Determine the type of view model used by this view.
            var viewModelType = default(Type);
            var viewModelTypeName = (String)viewElement.Attribute("ViewModelType");
            if (viewModelTypeName != null)
            {
                viewModelType = Type.GetType(viewModelTypeName, false);

                if (viewModelType == null)
                    throw new UvmlException(PresentationStrings.ViewModelTypeNotFound.Format(viewModelTypeName));

                var viewModelWrapperAttr = viewModelType.GetCustomAttributes(typeof(ViewModelWrapperAttribute), false)
                    .Cast<ViewModelWrapperAttribute>().SingleOrDefault();
                if (viewModelWrapperAttr != null)
                {
                    viewModelType = viewModelWrapperAttr.WrapperType;
                }
                else
                {
                    var viewModelWrapperName = PresentationFoundationView.GetDataSourceWrapperNameForView(uiPanelDefinition.AssetFilePath);
                    var viewModelWrapperType = uv.GetUI().GetPresentationFoundation().GetDataSourceWrapperTypeByName(viewModelWrapperName);
                    if (viewModelWrapperType == null)
                        throw new UvmlException(PresentationStrings.CannotFindViewModelWrapper.Format(viewModelWrapperName));

                    viewModelType = viewModelWrapperType;
                }
            }

            // Create a UVML template which will instantiate the view.
            AddUvmlAnnotations(viewModelType.Name, viewElement);
            var viewTemplate = new UvmlTemplate(viewElement, typeof(PresentationFoundationView), (puv, pname) =>
            {
                var view = new PresentationFoundationView(puv, uiPanel, viewModelType);
                var viewModel = vmfactory == null ? null : vmfactory(view);
                if (viewModel != null)
                {
                    view.SetViewModel(viewModel);
                }

                var root = view.LayoutRoot;
                root.BeginInit();

                var rootAdornerDecorator = new AdornerDecorator(puv, null);
                rootAdornerDecorator.BeginInit();
                root.Child = rootAdornerDecorator;

                var rootGridTemplate = CreateTemplateFromXml(puv, viewElement, null, typeof(Grid), cultureInfo);
                var rootGridContext = UvmlInstantiationContext.ForView(puv, view);

                var rootGridTemplateInstance = (UvmlTemplateInstance)rootGridTemplate.Instantiate(puv, rootGridContext);
                var rootGrid = (Grid)rootGridTemplateInstance.Finalize();

                rootAdornerDecorator.Child = rootGrid;

                return view;
            });

            // Instantiate the view template.
            var viewTemplateInstance = (UvmlTemplateInstance)viewTemplate.Instantiate(uv, null);
            var viewInstance = (PresentationFoundationView)viewTemplateInstance.Finalize();

            var viewRoot = viewInstance.LayoutRoot;
            var viewRootAdornerDecorator = (AdornerDecorator)viewRoot.Child;
            viewRootAdornerDecorator.EndInit();
            viewRoot.EndInit();
            viewRoot.CacheLayoutParameters();

            var viewInstanceViewModel = viewInstance.ViewModel;
            if (viewInstanceViewModel != null)
                viewInstance.Namescope.PopulateFieldsFromRegisteredElements(viewInstanceViewModel);

            return viewInstance;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PresentationFoundationViewResources"/> class.
        /// </summary>
        /// <param name="view">The view that owns this resource collection.</param>
        internal PresentationFoundationViewResources(PresentationFoundationView view)
        {
            Contract.Require(view, "view");

            this.view = view;
        }
Exemplo n.º 47
0
 /// <inheritdoc/>
 protected override void Execute(PresentationFoundationView view, Object parameter) => executeDelegate(view);
Exemplo n.º 48
0
        /// <summary>
        /// Searches the visual tree for the next element in the tab order after <paramref name="navElement"/>.
        /// </summary>
        /// <param name="view">The view for which navigation is being performed.</param>
        /// <param name="navContainer">The navigation container that contains <paramref name="navElement"/>.</param>
        /// <param name="navElement">The element at which to begin the search.</param>
        /// <param name="navProp">The navigation property to evaluate.</param>
        /// <param name="local">A value indicating whether the search is restricted only to the visual subtree of <paramref name="navContainer"/>.</param>
        /// <returns>The next element in the tab order after <paramref name="navElement"/>, or <c>null</c>.</returns>
        private static DependencyObject FindNextNavigationStop(PresentationFoundationView view, DependencyObject navContainer, DependencyObject navElement, DependencyProperty navProp, Boolean local)
        {
            if (navElement == null)
            {
                if (IsNavigationStop(navContainer))
                    return navContainer;

                var selectedTab = GetSelectedTab(navContainer as UIElement);
                if (selectedTab != null)
                    return FindNextNavigationStop(view, selectedTab, null, navProp, local);
            }

            if (navElement == null && IsNavigationStop(navContainer))
                return navContainer;

            var navMode = navContainer.GetValue<KeyboardNavigationMode>(navProp);

            if (navElement != null && (navMode == KeyboardNavigationMode.None || navMode == KeyboardNavigationMode.Once))
            {
                if (local || navContainer == view.LayoutRoot)
                    return null;

                var navContainerContainer = FindNavigationContainer(navContainer, navProp, false) ?? view.LayoutRoot;
                return FindNextNavigationStop(view, navContainerContainer, navContainer, navProp, local);
            }

            var first = default(DependencyObject);
            var next = navElement;

            while (true)
            {
                next = FindNextVisualElementWithinContainer(navContainer, next, navProp, navMode);

                if (next == null || next == first)
                    break;

                if (first == null)
                    first = next;

                var descendant = FindNextNavigationStop(view, next, null, navProp, true);
                if (descendant != null)
                    return descendant;

                if (navMode == KeyboardNavigationMode.Once)
                    navMode = KeyboardNavigationMode.Contained;
            }

            if (local)
                return null;

            if (navMode != KeyboardNavigationMode.Contained)
            {
                if (VisualTreeHelper.GetParent(navContainer) != null)
                {
                    var navContainerContainer = FindNavigationContainer(navContainer, navProp, false);
                    return FindNextNavigationStop(view, navContainerContainer, navContainer, navProp, false);
                }
                
                var firstNavStopInContainer = GetFirstNavigationStop(navContainer, navProp);
                return ((firstNavStopInContainer == null) ? null : FindNextVisualElementWithinContainer(firstNavStopInContainer, null, navProp, navMode)) ?? firstNavStopInContainer;
            }

            return null;
        }
Exemplo n.º 49
0
 /// <inheritdoc/>
 protected override void OnViewChanged(PresentationFoundationView oldView, PresentationFoundationView newView)
 {
     if (newView != null)
     {
         UpdateTextParserCache();
     }
     base.OnViewChanged(oldView, newView);
 }
Exemplo n.º 50
0
 /// <inheritdoc/>
 protected override bool CanExecute(PresentationFoundationView view, Object parameter) => canExecuteDelegate(view);
Exemplo n.º 51
0
 /// <summary>
 /// Captures the mouse within the specified input element.
 /// </summary>
 /// <param name="view">The view for which to set mouse capture.</param>
 /// <param name="element">The element to capture the mouse.</param>
 /// <returns><see langword="true"/> if the mouse was successfully captured; otherwise, <see langword="false"/>.</returns>
 public static Boolean Capture(PresentationFoundationView view, IInputElement element)
 {
     return Capture(view, element, CaptureMode.Element);
 }
        /// <summary>
        /// Enqueues any elements within the specified view that have invalid measure, arrange, or styles, but
        /// were previously removed from the queues as a result of being orphaned by a destroyed window.
        /// </summary>
        /// <param name="view">The view to restore.</param>
        internal void RestoreToQueues(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            RestoreToQueues(view.LayoutRoot);
        }
Exemplo n.º 53
0
        /// <summary>
        /// Gets the element which the mouse cursor is directly over within the specified view.
        /// </summary>
        /// <param name="view">The view to evaluate.</param>
        /// <returns>The element which is the mouse cursor is directly over within the specified view,
        /// or <see langword="null"/> if the mouse cursor is not over any element.</returns>
        public static IInputElement GetDirectlyOver(PresentationFoundationView view)
        {
            Contract.Require(view, nameof(view));

            return view.ElementUnderMouse;
        }
Exemplo n.º 54
0
 /// <summary>
 /// Creates an instantiation context for the specified view.
 /// </summary>
 /// <param name="uv">The Ultraviolet context.</param>
 /// <param name="view">The view for which to create an instantiation context.</param>
 /// <returns>The instantiation context which was created.</returns>
 internal static UvmlInstantiationContext ForView(UltravioletContext uv, PresentationFoundationView view)
 {
     return new UvmlInstantiationContext(uv, null, view, view.ViewModelType, view.Namescope);
 }