/// <summary> /// Creates an instance of ContentPresenterVisualHelper for the given ContentPresenter and ChildName, if necessary. /// Or returns an existing one from cache, if available. /// </summary> public static ContentPresenterVisualHelper GetInstance(ContentPresenter ContentPresenter, string ChildName) { string key = CreateKey(ContentPresenter, ChildName); var cachedObj = Cache.Get(key) as ContentPresenterVisualHelper; if (cachedObj != null) { return(cachedObj); } lock (CacheLock) { cachedObj = Cache.Get(key) as ContentPresenterVisualHelper; if (cachedObj != null) { return(cachedObj); } var obj = new ContentPresenterVisualHelper(ContentPresenter, ChildName); var cacheItem = new CacheItem(key, obj); var expiration = DateTimeOffset.Now + TimeSpan.FromSeconds(60); var policy = new CacheItemPolicy { AbsoluteExpiration = expiration }; Cache.Set(cacheItem, policy); return(obj); } }
/// <param name="parameter"> /// 1. Can be null/empty, in which case the first Visual Child of the ContentPresenter is returned by the Helper /// 2. Can be a string, in which case the ContentPresenter's child with the given name is returned /// </param> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) { return(null); } ContentPresenter cp = value as ContentPresenter; if (cp == null) { throw new InvalidOperationException(String.Format("value must be of type ContentPresenter, but was {0}", value.GetType().FullName)); } return(ContentPresenterVisualHelper.GetInstance(cp, parameter as string)); }