public void Add(KeyValuePair <object, object> item)
        {
            IDictionary <object, object> resources = GetOrCreateUnderlayingDictionary();

            resources.Add(item);
            MPF.SetOwner(item.Value, this, false);
            FireChanged();
        }
        internal void Add(object key, object value, bool fireChanged)
        {
            IDictionary <object, object> resources = GetOrCreateUnderlayingDictionary();

            resources.Add(key, value);
            MPF.SetOwner(value, this, false);
            if (fireChanged)
            {
                FireChanged();
            }
        }
        /// <summary>
        /// Takes over the control over the resources in the given resource dictionary. That means, the
        /// <see cref="DependencyObject.LogicalParent"/> properties of the resource keys and values will be set to this
        /// instance.
        /// </summary>
        /// <param name="dict">Resource dictionary whose contents should be taken over.</param>
        /// <param name="overwriteNames">If set to <c>true</c>, name collisions between the <paramref name="dict"/> and
        /// this dictionary will be ignored.</param>
        /// <param name="takeoverDictInstance">If set to <c>true</c>, the given <paramref name="dict"/> instance will be
        /// disposed by this method. Else, the <paramref name="dict"/> will be left untouched.</param>
        public void TakeOver(ResourceDictionary dict, bool overwriteNames, bool takeoverDictInstance)
        {
            bool wasChanged = false;

            foreach (KeyValuePair <object, object> entry in (IDictionary <object, object>)dict)
            {
                object key   = entry.Key;
                object value = entry.Value;
                Set(key, value, false);
                // Here we have the rare case that we must force to set the owner property to this instance to make the adopted resource really
                // belong to it.
                MPF.SetOwner(value, this, true);
                DependencyObject depObj = key as DependencyObject;
                if (depObj != null)
                {
                    depObj.LogicalParent = this;
                }
                depObj = value as DependencyObject;
                if (depObj != null)
                {
                    depObj.LogicalParent = this;
                }
                wasChanged = true;
            }
            if (dict._names != null)
            {
                foreach (KeyValuePair <string, object> kvp in dict._names)
                {
                    if (overwriteNames)
                    {
                        SetName(kvp.Key, kvp.Value);
                    }
                    else
                    {
                        RegisterName(kvp.Key, kvp.Value);
                    }
                }
            }
            if (takeoverDictInstance)
            {
                if (dict._resources != null)
                {
                    dict._resources.Clear();
                }
                dict.Dispose();
            }
            if (wasChanged)
            {
                FireChanged();
            }
        }
        internal void Set(object key, object value, bool fireChanged)
        {
            IDictionary <object, object> resources = GetOrCreateUnderlayingDictionary();
            object oldRes;

            if (resources.TryGetValue(key, out oldRes))
            {
                MPF.CleanupAndDisposeResourceIfOwner(oldRes, this);
            }
            resources[key] = value;
            MPF.SetOwner(value, this, false);
            if (fireChanged)
            {
                FireChanged();
            }
        }
        public static void RegisterUnmodifiableResourceDuringParsingProcess(IUnmodifiableResource resource, IParserContext context)
        {
            IEnumerator <ElementContextInfo> enumer = ((IEnumerable <ElementContextInfo>)context.ContextStack).GetEnumerator();

            if (!enumer.MoveNext())
            {
                return;
            }
            if (!enumer.MoveNext())
            {
                return;
            }
            ResourceDictionary rd = enumer.Current.Instance as ResourceDictionary;

            if (rd != null)
            {
                MPF.SetOwner(resource, rd, false);
            }
        }
        public override void DeepCopy(IDeepCopyable source, ICopyManager copyManager)
        {
            base.DeepCopy(source, copyManager);
            ResourceDictionary rd = (ResourceDictionary)source;

            Source = rd.Source;
            _dependsOnStyleResources = new List <string>(rd._dependsOnStyleResources);
            _mergedDictionaries      = rd._mergedDictionaries; // MergedDictionaries won't be copied as they come from outside the application, see the WPF docs for that property
            if (rd._names == null)
            {
                _names = null;
            }
            else
            {
                _names = new Dictionary <string, object>(rd._names.Count);
                foreach (KeyValuePair <string, object> kvp in rd._names)
                {
                    if (_names.ContainsKey(kvp.Key))
                    {
                        continue;
                    }
                    else
                    {
                        _names.Add(kvp.Key, copyManager.GetCopy(kvp.Value));
                    }
                }
            }
            if (rd._resources == null)
            {
                _resources = null;
            }
            else
            {
                _resources = new Dictionary <object, object>(rd._resources.Count);
                foreach (KeyValuePair <object, object> kvp in rd._resources)
                {
                    object valueCopy = copyManager.GetCopy(kvp.Value);
                    _resources.Add(copyManager.GetCopy(kvp.Key), valueCopy);
                    MPF.SetOwner(valueCopy, this, false);
                }
            }
        }
예제 #7
0
 void OnValueChanged(AbstractProperty prop, object oldVal)
 {
     MPF.CleanupAndDisposeResourceIfOwner(oldVal, this);
     MPF.SetOwner(Value, this, false);
 }