public static PopupItem GetPopupItem(Graphic graphic, Layer layer, IEnumerable <FieldInfo> fields, LayerInformation layerInfo, string layerName, string title = null, int?layerId = null)
        {
            if (layerInfo != null || layer is GraphicsLayer)
            {
                PopupItem popupItem = new PopupItem()
                {
                    Graphic   = graphic,
                    Layer     = layer,
                    Title     = title,
                    LayerName = layerName,
                };
                if (layerId.HasValue)
                {
                    popupItem.LayerId = layerId.Value;
                }

                if (layerInfo != null)
                {
                    GetMappedAttributes(graphic.Attributes, layerInfo);
                }

                popupItem.FieldInfos = MapTipsHelper.ToFieldSettings(fields);
                MapTipsHelper.GetTitle(popupItem, layerInfo);

                bool         hasContent = true;
                DataTemplate dt         = MapTipsHelper.BuildMapTipDataTemplate(popupItem, out hasContent, layerInfo);
                if (!hasContent)
                {
                    popupItem.DataTemplate = null;
                }
                else if (dt != null)
                {
                    popupItem.DataTemplate = dt;
                }

                if (hasContent || (!string.IsNullOrWhiteSpace(popupItem.Title)))
                {
                    return(popupItem);
                }
            }
            return(null);
        }
        internal static void rebuildMapTipContentsBasedOnFieldVisibility(HoverResults results)
        {
            HoverResults hr = results as HoverResults;

            if (hr == null)
            {
                return;
            }
            GraphicsLayer hrLayer = hr.Layer;

            if (hr.Graphic == null || hr.Graphic.Attributes == null || hrLayer == null)
            {
                hr.Visibility = Visibility.Collapsed;
                return;
            }
            Visibility hrVisibility = Visibility.Visible;

            IEnumerable <FieldInfo> fields = ESRI.ArcGIS.Mapping.Core.LayerExtensions.GetFields(hrLayer); // Store the fields on the Map Tips element
            string displayField            = ESRI.ArcGIS.Mapping.Core.LayerExtensions.GetDisplayField(hrLayer);

            if (string.IsNullOrEmpty(displayField))
            {
                displayField = FieldInfo.GetDefaultDisplayField(fields);
                ESRI.ArcGIS.Mapping.Core.LayerExtensions.SetDisplayField(hrLayer, displayField);
            }
            Graphic   popupGraphic = hr.Graphic;
            PopupItem popupItem    = new PopupItem()
            {
                DataTemplate    = null,
                Graphic         = popupGraphic,
                Layer           = hrLayer,
                LayerName       = ESRI.ArcGIS.Mapping.Core.LayerExtensions.GetLayerName(hrLayer),
                Title           = null,
                TitleExpression = null,
            };

            popupItem.FieldInfos = MapTipsHelper.ToFieldSettings(ESRI.ArcGIS.Mapping.Core.LayerExtensions.GetFields(hrLayer));
            MapTipsHelper.GetTitle(popupItem, null);

            bool hasContent = false;

            popupItem.DataTemplate = MapTipsHelper.BuildMapTipDataTemplate(popupItem, out hasContent);
            if (!hasContent)
            {
                popupItem.DataTemplate = null;
            }

            ESRI.ArcGIS.Client.Extensibility.PopupInfo popupInfo = new ESRI.ArcGIS.Client.Extensibility.PopupInfo()
            {
                AttributeContainer = hr.AttributeContainer,
                Container          = hr,
                PopupItem          = popupItem
            };
            hr.PopupInfo = popupInfo;

            bool hasTitle = !string.IsNullOrEmpty(popupInfo.PopupItem.Title);

            if (hasTitle || hasContent)
            {
                hrVisibility = Visibility.Visible;
                // re-establish delay
                TimeSpan delay = (TimeSpan)hr.Map.GetValue(DelayMapTipHide.HideDelayProperty);
                hrLayer.MapTip.SetValue(GraphicsLayer.MapTipHideDelayProperty, delay);
            }
            else
            {
                hrVisibility = Visibility.Collapsed;
            }
            hr.Visibility = hrVisibility;
        }