コード例 #1
0
        /// <summary>
        /// Called when the viewmodel request a view service.
        /// </summary>
        private void OnViewModel_ViewServiceRequest(object sender, ViewServiceRequestEventArgs e)
        {
            // Check for an implementation on the hosted control
            if (TryFillViewServiceImplementation(m_host, e))
            {
                return;
            }

            // Try to walk the visual tree up until we find a view service implementation
            DependencyObject actParent        = VisualTreeHelper.GetParent(m_host);
            FrameworkElement actParentElement = actParent as FrameworkElement;

            while ((actParent != null) && (actParentElement == null))
            {
                if ((actParentElement != null) &&
                    (TryFillViewServiceImplementation(actParentElement, e)))
                {
                    return;
                }


                actParent        = VisualTreeHelper.GetParent(m_host);
                actParentElement = actParent as FrameworkElement;
            }
        }
コード例 #2
0
        private bool TryFillViewServiceImplementation(FrameworkElement element, ViewServiceRequestEventArgs e)
        {
            ViewServiceCollection viewServices = element.GetValue(
                Interaction.ViewServicesProperty) as ViewServiceCollection;

            if (viewServices == null)
            {
                return(false);
            }

            // Try to find the view service within the host control
            foreach (object actViewService in viewServices)
            {
                if (actViewService == null)
                {
                    continue;
                }

                Type actViewServiceType = actViewService.GetType();
                if (e.RequestedType.GetTypeInfo().IsAssignableFrom(actViewServiceType.GetTypeInfo()))
                {
                    e.Implementation = actViewService;
                    return(true);
                }
            }

            return(false);
        }