public void ReadOnlyDependencyPropertiesTest()
        {
            ReadOnlyElement element = new ReadOnlyElement();

            Assert.AreEqual(0, element.Value1);

            element.SetValue(ReadOnlyElement._private_Value1Key, 1);
            Assert.AreEqual(1, element.Value1);

            try
            {
                element.SetValue(ReadOnlyElement.Value1Property, 2);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is Exception);
            }

            try
            {
                IDependencyPropertyValueEntry entry = element.GetValueEntry(ReadOnlyElement.Value1Property);
                entry.SetBaseValue((int)BaseValueSource.Local, 3);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is Exception);
            }

            try
            {
                DependencyPropertyKey fakeKey = new DependencyPropertyKey(ReadOnlyElement.Value1Property);
                element.SetValue(fakeKey, 4);
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is Exception);
            }

            try
            {
                DependencyPropertyKey fakeKey = DependencyProperty.RegisterReadOnly("Value1", typeof(int), typeof(ReadOnlyElement), new PropertyMetadata());
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsTrue(e is Exception);
            }
        }
        private void SetValue(DependencyProperty dependencyProperty, DependencyPropertyKey dependencyPropertyKey, object value, bool setCurrentValue = false, BaseValueSource source = BaseValueSource.Unknown)
        {
            VerifyReadOnlyProperty(dependencyProperty, dependencyPropertyKey);

            IExpressionProvider newExpressionProvider = value as IExpressionProvider;

            if (newExpressionProvider == null && !dependencyProperty.IsValidValue(value))
            {
                return; // invalid value
            }

            IDependencyPropertyValueEntry entry = GetInitializedValueEntry(dependencyProperty);

            IExpression oldExpression = setCurrentValue ?
                                        entry.GetBaseValue(false) as IExpression : // current value may be set in the top priority expression
                                        entry.GetBaseValue((int)source, false) as IExpression;

            if (newExpressionProvider != null)
            {
                value = newExpressionProvider.CreateExpression(this, dependencyProperty);
            }
            else if (oldExpression != null && oldExpression.SetValue(value))
            {
                return; // value (current or not) was set in the existing expression, nothing else to do
            }

            if (setCurrentValue)
            {
                entry.SetCurrentValue(value);
                return; // base value isn't changed
            }

            if (oldExpression is IDisposable) // expression is being replaced
            {
                ((IDisposable)oldExpression).Dispose();
            }

            entry.SetBaseValue((int)source, value);
            entry.ClearCurrentValue();
        }
Exemple #3
0
 public void SetBaseValue(int priority, object value)
 {
     source.SetBaseValue(priority, value);
 }