GetDefaultValue() private method

private GetDefaultValue ( Kind kind ) : object
kind Kind
return object
Example #1
0
        static void InvokeChangedCallback(DependencyObject obj, DependencyProperty property, PropertyChangedCallback callback,
                                          object old_obj, object new_obj)
        {
            if (old_obj == null && property.property_type.IsValueType && !property.IsNullable)
            {
                old_obj = property.GetDefaultValue(obj);
                Console.WriteLine("WARNING: Got a null value for {0}.{1} which is a value type", property.DeclaringType.Name, property.Name);
            }

            if (Helper.AreEqual(property.PropertyType, old_obj, new_obj))
            {
                return;
            }

            var args = new DependencyPropertyChangedEventArgs(old_obj, new_obj, property);

            // note: since callbacks might throw exceptions but we cannot catch them
            callback(obj, args);
        }
Example #2
0
		internal void SetValueImpl (DependencyProperty dp, object value)
		{
			bool updateTwoWay = false;
			bool addingExpression = false;
			Expression existing;
			Expression expression = value as Expression;
			BindingExpressionBase bindingExpression = expression as BindingExpressionBase;
			
			if (bindingExpression != null) {
				string path = bindingExpression.Binding.Path.Path;
				if ((string.IsNullOrEmpty (path) || path == ".") &&
				    bindingExpression.Binding.Mode == BindingMode.TwoWay)
					throw new ArgumentException ("TwoWay bindings require a non-empty Path");
				bindingExpression.Binding.Seal ();
			}

			expressions.TryGetValue (dp, out existing);
			
			if (expression != null) {
				if (existing != expression) {
					if (expression.Attached)
						throw new ArgumentException ("Cannot attach the same Expression to multiple FrameworkElements");

					if (existing != null)
						RemoveExpression (dp);
					expressions.Add (dp, expression);
					expression.OnAttached (this);
				}
				addingExpression = true;
				value = expression.GetValue (dp);
			} else if (existing != null) {
				if (existing is BindingExpressionBase) {
					BindingExpressionBase beb = (BindingExpressionBase)existing;

					if (beb.Binding.Mode == BindingMode.TwoWay) {
						updateTwoWay = !beb.Updating && !(dp is CustomDependencyProperty);
					} else if (!beb.Updating || beb.Binding.Mode == BindingMode.OneTime) {
						RemoveExpression (dp);
					}
				}
				else if (!existing.Updating) {
					RemoveExpression (dp);
				}
			}

			try {
				NativeDependencyObjectHelper.SetValue (this, dp, value);
				if (updateTwoWay)
					((BindingExpressionBase)existing).TryUpdateSourceObject (value);
			} catch {
				if (!addingExpression)
					throw;
				else {
					NativeDependencyObjectHelper.SetValue (this, dp, dp.GetDefaultValue (this));
					if (updateTwoWay)
						((BindingExpressionBase)existing).TryUpdateSourceObject (value);
				}
			}
		}
 /// <summary>
 ///     Called to evaluate the Expression value
 /// </summary>
 /// <param name="d">DependencyObject being queried</param>
 /// <param name="dp">Property being queried</param>
 /// <returns>Computed value. Default (of the target) if unavailable.</returns>
 internal override object GetValue(DependencyObject d, DependencyProperty dp)
 {
     return dp.GetDefaultValue(d.DependencyObjectType);
 }
Example #4
0
 /// <summary>
 ///     Called to evaluate the Expression value
 /// </summary>
 /// <param name="d">DependencyObject being queried</param>
 /// <param name="dp">Property being queried</param>
 /// <returns>Computed value. Default (of the target) if unavailable.</returns>
 internal override object GetValue(DependencyObject d, DependencyProperty dp)
 {
     return(dp.GetDefaultValue(d.DependencyObjectType));
 }
Example #5
0
		object ConvertToType (DependencyProperty dp, object value)
		{
			try {
				if (!PropertyPathWalker.IsPathBroken && Binding.Converter != null) {
					value = Binding.Converter.Convert (
						value,
						Property.PropertyType,
						Binding.ConverterParameter,
						GetConverterCulture ()
					);
				}

				if (value == DependencyProperty.UnsetValue || PropertyPathWalker.IsPathBroken) {
					value = Binding.FallbackValue ?? dp.GetDefaultValue (Target);
				}
				else if (value == null) {
					value = Binding.TargetNullValue;
					if (value == null && IsBoundToAnyDataContext && string.IsNullOrEmpty (Binding.Path.Path))
						value = dp.GetDefaultValue (Target);
				} else {
					string format = Binding.StringFormat;
					if (!string.IsNullOrEmpty (format)) {
						if (!format.Contains ("{0"))
							format = "{0:" + format + "}";
						value = string.Format (GetConverterCulture (), format, value);
					}
				}

				if (value != null) {
					value = ConvertFromSourceToTarget (value);
				}
			} catch (Exception ex) {
				return MoonlightTypeConverter.ConvertObject (dp, Binding.FallbackValue, Target.GetType (), true);
			}
			return value;
		}
Example #6
0
        static void InvokeChangedCallback(DependencyObject obj, DependencyProperty property, PropertyChangedCallback callback,
						   object old_obj, object new_obj)
        {
            if (old_obj == null && property.property_type.IsValueType && !property.IsNullable) {
                old_obj = property.GetDefaultValue (obj);
                Console.WriteLine ("WARNING: Got a null value for {0}.{1} which is a value type", property.DeclaringType.Name, property.Name);
            }

            if (Helper.AreEqual (property.PropertyType, old_obj, new_obj))
                return;

            var args = new DependencyPropertyChangedEventArgs (old_obj, new_obj, property);

            // note: since callbacks might throw exceptions but we cannot catch them
            callback (obj, args);
        }
Example #7
0
		internal override object GetValue (DependencyProperty dp)
		{
			if (cached)
				return cachedValue;

			cached = true;
			if (PropertyPathWalker.IsPathBroken) {
				cachedValue = null;
			}
			else {
				cachedValue = PropertyPathWalker.Value;
			}
			try {
				cachedValue = ConvertToType (dp, cachedValue);
			} catch {
				cachedValue = dp.GetDefaultValue (Target);
			}
			
			return cachedValue;
		}
Example #8
0
		internal override object GetValue (DependencyProperty dp)
		{
			if (cached)
				return cachedValue;

			cached = true;
			if (DataSource == null) {
				cachedValue = dp.GetDefaultValue (Target);
				return cachedValue;
			}
			else if (PropertyPathWalker.IsPathBroken) {
				// If it the path is broken, don't call the converter unless we use the fallback.
				// FIXME: Add an explicit test on this.
				if (Binding.FallbackValue == null) {
					cachedValue = dp.GetDefaultValue (Target);
					return cachedValue;
				}
				cachedValue = Binding.FallbackValue;
			}
			else {
				cachedValue = PropertyPathWalker.Value;
			}
			try {
				cachedValue = ConvertToType (dp, cachedValue);
			} catch {
				cachedValue  = dp.GetDefaultValue (Target);
			}
			
			return cachedValue;
		}
Example #9
0
 private object GetConvertedFallbackOrDefaultValue(DependencyObject d, DependencyProperty dp)
 {
     if (this.binding.FallbackValue != null)
     {
         return this.ConvertedFallbackValue;
     }
     return dp.GetDefaultValue(d);
 }
Example #10
0
 internal override object GetValue(DependencyObject d, DependencyProperty dp)
 {
     if (this.listener != null && this.listener.FullPathExists)
     {
         return this.ConvertToTarget(this.listener.LeafValue);
     }
     if (this.listener == null || !this.binding.Path.IsPathToSource)
     {
         return this.GetConvertedFallbackOrDefaultValue(d, dp);
     }
     if (this.binding.TargetNullValue != null)
     {
         return this.ConvertToTarget(null);
     }
     return dp.GetDefaultValue(d);
 }
Example #11
0
        internal void SetValueImpl(DependencyProperty dp, object value)
        {
            if (value == DependencyProperty.UnsetValue)
            {
                ClearValue(dp);
                return;
            }

            bool                  updateTwoWay      = false;
            bool                  addingExpression  = false;
            Expression            existing          = null;
            Expression            expression        = value as Expression;
            BindingExpressionBase bindingExpression = expression as BindingExpressionBase;

            if (bindingExpression != null)
            {
                string path = bindingExpression.Binding.Path.Path;
                if ((string.IsNullOrEmpty(path) || path == ".") &&
                    bindingExpression.Binding.Mode == BindingMode.TwoWay)
                {
                    throw new ArgumentException("TwoWay bindings require a non-empty Path");
                }
                bindingExpression.Binding.Seal();
            }

            if (expressions != null)
            {
                if (!expressions.TryGetValue(dp, out existing))
                {
                    existing = null;
                }
            }

            if (expression != null)
            {
                if (existing != expression)
                {
                    if (expression.Attached)
                    {
                        throw new ArgumentException("Cannot attach the same Expression to multiple FrameworkElements");
                    }

                    if (existing != null)
                    {
                        RemoveExpression(dp);
                    }
                    if (expressions == null)
                    {
                        expressions = new Dictionary <DependencyProperty, Expression> ();
                    }

                    expressions.Add(dp, expression);
                    expression.OnAttached(this);
                }
                addingExpression = true;
                value            = expression.GetValue(dp);
            }
            else if (existing != null)
            {
                if (existing is BindingExpressionBase)
                {
                    BindingExpressionBase beb = (BindingExpressionBase)existing;

                    if (beb.Binding.Mode == BindingMode.TwoWay)
                    {
                        updateTwoWay = !beb.Updating && !(dp is CustomDependencyProperty);
                    }
                    else if (!beb.Updating || beb.Binding.Mode == BindingMode.OneTime)
                    {
                        RemoveExpression(dp);
                    }
                }
                else if (!existing.Updating)
                {
                    RemoveExpression(dp);
                }
            }

            try {
                NativeDependencyObjectHelper.SetValue(this, dp, value);
                if (updateTwoWay)
                {
                    ((BindingExpressionBase)existing).TryUpdateSourceObject(value);
                }
            } catch {
                if (!addingExpression)
                {
                    throw;
                }
                else
                {
                    NativeDependencyObjectHelper.SetValue(this, dp, dp.GetDefaultValue(this));
                    if (updateTwoWay)
                    {
                        ((BindingExpressionBase)existing).TryUpdateSourceObject(value);
                    }
                }
            }
        }