public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var ctxt = (container as FrameworkElement);
            if (ctxt == null) return null;

            if (item == null)
            {
                //System.Diagnostics.Trace.WriteLine("Warning: Null item in data template selector");
                return ctxt.FindResource("Empty") as DataTemplate;
            }

            // Allow type-specific data templates to take precedence (should we)?
            var key = new DataTemplateKey(item.GetType());
            var typeTemplate = ctxt.TryFindResource(key) as DataTemplate;
            if (typeTemplate != null)
                return typeTemplate;

            // Common problem if the MEF import failed to find any suitable DLLs
            if (!Renderers.Any())
                System.Diagnostics.Trace.WriteLine("Warning: No visualizer components loaded");

            var template = "";
            var r = Renderers
                .OrderByDescending(i => i.Importance)
                .FirstOrDefault(i => (i.CanRender(item, ref template)));
            if (r == null || String.IsNullOrEmpty(template))
            {
                System.Diagnostics.Trace.WriteLine("Warning: No renderers that can handle object");
                return ctxt.FindResource("Default") as DataTemplate;
            }

            return ctxt.TryFindResource(template) as DataTemplate ?? ctxt.FindResource("Missing") as DataTemplate;
        }
        public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
        {
            if (item == null)
                return base.SelectTemplate(item, container);

            var mi = item as ModelItem;
            if (mi == null)
                return DefaultDataTemplate;

            FrameworkElement fe = null;
            // CP may be in a template or may be on its own.
            if (container is ContentPresenter)
                fe = ((container as ContentPresenter).TemplatedParent ??
                      (container as ContentPresenter).Parent) as FrameworkElement;
            else
                fe = container as FrameworkElement;

            if (fe == null)
                return null;
            var key = new System.Windows.DataTemplateKey(mi.ItemType);

            return fe.TryFindResource(key) as DataTemplate ??
                   fe.TryFindResource(mi.ItemType) as DataTemplate ??
                   fe.TryFindResource(mi.ItemType.FullName) as DataTemplate ??
                   DefaultDataTemplate;
        }
        // ******************************************************************
        void ObjCollCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            // Find the DataTemplate
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                foreach (object obj in e.NewItems)
                {
                    // Here th obj Type is the key to the resource, it works but
                    var key          = new System.Windows.DataTemplateKey(obj.GetType());
                    var dataTemplate = (DataTemplate)DockSite.FindResource(key);

                    var userControl = dataTemplate.LoadContent() as UserControl;
                    if (userControl != null)
                    {
                        userControl.DataContext = obj;

                        var documentWindow = new DocumentWindow(DockSite, null, "Title from viemodel", null, userControl);
                        documentWindow.Description = "viewModel.Description";

                        // Activate the document
                        documentWindow.Activate();
                    }
                }
            }
        }
Example #4
0
 private void RegisterDataTemplate(Type viewType, Type viewModelType)
 {
     var dataTemplateKey = new DataTemplateKey(viewModelType);
     var dataTemplate = new DataTemplate(viewModelType)
         {
             VisualTree = new FrameworkElementFactory(viewType),
         };
     Resources.Add(dataTemplateKey, dataTemplate);
 }
 public override DataTemplate SelectTemplate(object item, DependencyObject container)
 {
     var dataTemplate = base.SelectTemplate(item, container);
     if (dataTemplate == null && Type != null && Type.IsAssignableFrom(item.GetType()))
     {
         var key = new DataTemplateKey(Type);
         FrameworkElement element = container as FrameworkElement;
         dataTemplate = (DataTemplate)element.FindResource(key);
     }
     return dataTemplate;
 }
		protected DataTemplate SelectTamplate(Type type, DependencyObject container)
		{
			DataTemplate template = null;
			if (type != null)
			{
				FrameworkElement element = container as FrameworkElement;
				var key = new DataTemplateKey(type);
				template = (DataTemplate)element.FindResource(key);
			}
			return template;
		}
Example #7
0
 private void AddWindowBindings(Window window, Dictionary<Type, Type> viewViewModelBindings)
 {
     foreach (var singleBinding in viewViewModelBindings)
     {
         DataTemplate dt = new DataTemplate();
         dt.DataType = singleBinding.Value;
         FrameworkElementFactory fef = new FrameworkElementFactory(singleBinding.Key);
         dt.VisualTree = fef;
         DataTemplateKey dtKey = new DataTemplateKey(singleBinding.Value);
         window.Resources.Add(dtKey, dt);
     }
 }
        private DataTemplate FindDataTemplate(Property property, ResourceDictionary templates)
        {
            DataTemplate template = null;

            string propType = property.PropertyType.FullName;

            if (propType.Equals(typeof(TraceLabSDK.Component.Config.FilePath).FullName) == true ||
                propType.Equals(typeof(TraceLabSDK.Component.Config.DirectoryPath).FullName) == true)
            {
                  object dataTempalteKey = new DataTemplateKey(property.PropertyType);
                  template = templates[dataTempalteKey] as DataTemplate;
            }
            return template;
        }
Example #9
0
        /// <summary>
        /// Creates or identifies the element that is used to display the given item.
        /// </summary>
        /// <returns>The element that is used to display the given item.</returns>
        protected override DependencyObject GetContainerForItemOverride()
        {
            if (_currentItem != null)
            {
                var type = _currentItem.GetType();
                _currentItem = null;

                // Manually load implicit data template. (Otherwise the ItemsControl will create a
                // ContentPresenter.)
                var dataTemplateKey = new DataTemplateKey(type);
                var dataTemplate = TryFindResource(dataTemplateKey) as DataTemplate;
                if (dataTemplate != null)
                    return dataTemplate.LoadContent();
            }

            return base.GetContainerForItemOverride();
        }
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            Type type = null;
            DataTemplate template = null;
            DependencyObject root = GetLogicalRoot(container);

            if (root is ApplicationView)
            {
                if (item is ShellViewModel)
                    type = typeof(ShellViewModel);
            }
            else if (root is DialogView)
            {
                if (item is SaveCancelDialogViewModel)
                    type = typeof(SaveCancelDialogViewModel);
                else if (item is MessageBoxViewModel)
                    type = typeof(MessageBoxViewModel);
            }
            else if (root is HeaderedWindowView)
            {
                if (item is DialogViewModel)
                    type = typeof(DialogViewModel);
                else if (item is ApplicationViewModel)
                    type = typeof(ApplicationViewModel);
            }
            else if (root is WindowBaseView)
            {
                if (item is HeaderedWindowViewModel)
                    type = typeof(HeaderedWindowViewModel);
            }

            if (type != null)
            {
                FrameworkElement element = container as FrameworkElement;
                var key = new DataTemplateKey(type);
                template = (DataTemplate)element.FindResource(key);
            }
            return template ?? base.SelectTemplate(item, container);
        }
        public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
        {
            if (item == null)
            {
                return(base.SelectTemplate(item, container));
            }

            var mi = item as ModelItem;

            if (mi == null)
            {
                return(DefaultDataTemplate);
            }

            FrameworkElement fe = null;

            // CP may be in a template or may be on its own.
            if (container is ContentPresenter)
            {
                fe = ((container as ContentPresenter).TemplatedParent ??
                      (container as ContentPresenter).Parent) as FrameworkElement;
            }
            else
            {
                fe = container as FrameworkElement;
            }

            if (fe == null)
            {
                return(null);
            }
            var key = new System.Windows.DataTemplateKey(mi.ItemType);

            return(fe.TryFindResource(key) as DataTemplate ??
                   fe.TryFindResource(mi.ItemType) as DataTemplate ??
                   fe.TryFindResource(mi.ItemType.FullName) as DataTemplate ??
                   DefaultDataTemplate);
        }
Example #12
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var key          = new System.Windows.DataTemplateKey(typeof(X));
            var dataTemplate = (DataTemplate)this.FindResource(key);

            if (dataTemplate != null)
            {
                var tc = dataTemplate.LoadContent().GetType();
                System.Console.WriteLine();
            }

            var items = icontrol1.Items;

            for (var i = 0; i < items.Count; ++i)
            {
                var item = icontrol1.ItemContainerGenerator.ContainerFromIndex(i);

                if (item is ContentPresenter cp)
                {
                    var template = cp.ContentTemplate;
                    System.Diagnostics.Debug.WriteLine($"template is {template?.ToString() ?? "NULL"}");
                }
            }
        }
Example #13
0
        // Searches through resource dictionaries to find a [Data|Table]Template 
        //  that matches the type of the 'item' parameter.  Failing an exact 
        //  match of the type, return something that matches one of its parent
        //  types. 
        internal static object FindTemplateResourceInternal(DependencyObject target, object item, Type templateType)
        {
            // Data styling doesn't apply to UIElement (bug 1007133).
            if (item == null || (item is UIElement)) 
            {
                return null; 
            } 

            Type type; 
            object dataType = ContentPresenter.DataTypeForItem(item, target, out type);

            ArrayList keys = new ArrayList();
 
            // construct the list of acceptable keys, in priority order
            int exactMatch = -1;    // number of entries that count as an exact match 
 
            // add compound keys for the dataType and all its base types
            while (dataType != null) 
            {
                object key = null;
                if (templateType == typeof(DataTemplate))
                    key = new DataTemplateKey(dataType); 

                if (key != null) 
                    keys.Add(key); 

                // all keys added for the given item type itself count as an exact match 
                if (exactMatch == -1)
                    exactMatch = keys.Count;

                if (type != null) 
                {
                    type = type.BaseType; 
                    if (type == typeof(Object))     // don't search for Object - perf 
                        type = null;
                } 

                dataType = type;
            }
 
            int bestMatch = keys.Count; // index of best match so far
 
            // Search the parent chain 
            object resource = FindTemplateResourceInTree(target, keys, exactMatch, ref bestMatch);
 
            if (bestMatch >= exactMatch)
            {
                // Exact match not found in the parent chain.  Try App and System Resources.
                object appResource = Helper.FindTemplateResourceFromAppOrSystem(target, keys, exactMatch, ref bestMatch); 

                if (appResource != null) 
                    resource = appResource; 
            }
 
            return resource;
        }
		/// <summary>
		/// Selects a data template, through cache, by type, interface, or hiearchy (Depending on the DiscoveryMethod property
		/// </summary>
		/// <param name="itemType">The item type to look for</param>
		/// <param name="container">The items container</param>
		/// <returns>The selected DataTemplate or null if not found</returns>
		private DataTemplate SelectThroughCacheByType(Type itemType, FrameworkElement container)
		{
			DataTemplate dataTemplate = null;
			DataTemplateKey dataTemplateKey = new DataTemplateKey(itemType);
			if (!_cachedDataTemplates.TryGetValue(dataTemplateKey, out dataTemplate))
			{
				dataTemplate = SelectByType(itemType, container);
				if (dataTemplate == null)
				{
					_cachedDataTemplates.Add(dataTemplateKey, NullDataTemplate.Instance);
				}
				else
				{
					_cachedDataTemplates.Add(dataTemplateKey, dataTemplate);
				}
			}
			else if (dataTemplate is NullDataTemplate)
			{
				return null;
			}
			return dataTemplate;
		}
		/// <summary>
		/// Selects a data template by type, interface, or hiearchy (Depending on the DiscoveryMethod property
		/// </summary>
		/// <param name="itemType">The item type to look for</param>
		/// <param name="container">The items container</param>
		/// <returns>The selected DataTemplate or null if not found</returns>
		private DataTemplate SelectByType(Type itemType, FrameworkElement container)
		{
			DataTemplate dataTemplate = null;
			if (container == null)
			{
				return null;
			}
			if ((this.DiscoveryMethod & DiscoveryMethods.Type) == DiscoveryMethods.Type)
			{
				DataTemplateKey dataTemplateKey = new DataTemplateKey(itemType);
				dataTemplate = SelectByKey(dataTemplateKey, container);
			}
			if (dataTemplate == null)
			{
				if ((this.DiscoveryMethod & DiscoveryMethods.Interface) == DiscoveryMethods.Interface)
				{
					Type[] interfaces = itemType.GetInterfaces();
					for (int i = interfaces.Length - 1; i >= 0; i--)
					{
						Type interfaceType = interfaces[i];
						DataTemplateKey dataTemplateKey = new DataTemplateKey(interfaceType);
						dataTemplate = SelectByKey(dataTemplateKey, container);
						if (dataTemplate != null)
						{
							break;
						}
					}
				}
			}
			if (dataTemplate == null)
			{
				if ((this.DiscoveryMethod & DiscoveryMethods.Hierarchy) == DiscoveryMethods.Hierarchy)
				{
					dataTemplate = SelectByTypeHierachy(itemType.BaseType, container);
				}
			}
			return dataTemplate;
		}
		/// <summary>
		/// selects a DataTemplate by scanning the Type hierachy using a DataTemplateKey
		/// </summary>
		/// <param name="type">The item type to look for</param>
		/// <param name="container">The items container</param>
		/// <returns>The selected DataTemplate or null if not found</returns>
		private DataTemplate SelectByTypeHierachy(Type type, FrameworkElement container)
		{
			DataTemplate dataTemplate = null;
			while (dataTemplate == null && type != typeof(object))
			{
				DataTemplateKey dataTemplateKey = new DataTemplateKey(type);
				dataTemplate = SelectByKey(dataTemplateKey, container);
				type = type.BaseType;
			}
			return dataTemplate;
		}
Example #17
0
 private static DataTemplateKey GetResourceKey(object item)
 {
     XmlDataProvider xml = item as XmlDataProvider;
     DataTemplateKey key;
     if (xml != null)
     {
         key = new DataTemplateKey(xml.XPath);
     }
     else
     {
         XmlNode node = item as XmlNode;
         if (node != null)
         {
             key = new DataTemplateKey(node.Name);
         }
         else
         {
             key = new DataTemplateKey(item.GetType());
         }
     }
     return key;
 }
Example #18
0
        protected override DependencyObject GetContainerForItemOverride()
        {
            if (_currentItem != null)
            {
                var type = _currentItem.GetType();
                _currentItem = null;

                // Manually load implicit data template. (Otherwise the ItemsControl will create a
                // ContentPresenter.)
                var dataTemplateKey = new DataTemplateKey(type);
                var dataTemplate = TryFindResource(dataTemplateKey) as DataTemplate;
                var container = dataTemplate?.LoadContent();
                if (container is DockAnchorPane || container is DockSplitPane || container is DockTabPane)
                    return container;
            }

            // Fix for Visual Studio Designer.
            if (WindowsHelper.IsInDesignMode)
                return base.GetContainerForItemOverride();

            throw new DockException("Items in DockSplitPane need to be of type DockAnchorPane/DockSplitPane/DockTabPane "
                                    + "or need to have an implicit data template of the given type.");
        }
Example #19
0
 /// <summary>
 /// Creates the view for a model.
 /// </summary>
 /// <param name="modelType">Type of the model.</param>
 public static DependencyObject CreateViewForModel(Type modelType)
 {
     var key = new DataTemplateKey(modelType);
     var r = (DataTemplate)Application.Current.FindResource(key);
     if (r == null)
         throw new InvalidOperationException($"DataTemplate not found for '{modelType.FullName}'.");
     var c = r.LoadContent();
     return c;
 }
Example #20
0
		IEnumerable<string> GetDataTemplateStrings(FrameworkElement fwElem) {
			var obj = Page.GetStringsObject();
			if (obj == null)
				return Array.Empty<string>();

			var uiElem = obj as UIElement;
			if (uiElem != null)
				return GetStrings(uiElem);

			var key = new DataTemplateKey(obj as Type ?? obj.GetType());
			var dt = fwElem.TryFindResource(key) as DataTemplate;
			if (dt == null)
				return Array.Empty<string>();

			return GetStrings(dt.LoadContent());
		}