public object ProvideValue( IMarkupExtensionContext context ) { if (null == Converter) throw new InvalidOperationException("Converter is null"); if ( null == Value ) return null; PropertyInfo propertyInfo = context.Object.GetType( ).GetProperty( context.PropertyName ); Type propertyType = propertyInfo.PropertyType; Type valueType = Value.GetType( ); Type firstType = Converter.FirstType; Type secondType = Converter.SecondType; if ( firstType.IsAssignableFrom( propertyType ) && secondType.IsAssignableFrom( valueType ) ) { ConversionResult conversionResult = Converter.ConvertBack( Value ); if ( !conversionResult.Success) throw new InvalidOperationException(string.Format( "Cannot convert value : {0}", conversionResult.FailReason)); return conversionResult.Value; } else if ( firstType.IsAssignableFrom( valueType ) && secondType.IsAssignableFrom( propertyType ) ) { ConversionResult conversionResult = Converter.Convert( Value ); if ( !conversionResult.Success ) throw new InvalidOperationException( string.Format( "Cannot convert value : {0}", conversionResult.FailReason ) ); return conversionResult.Value; } else { throw new InvalidOperationException( string.Format("Cannot use specified converter to convert {0} to {1}", valueType.Name, propertyType.Name)); } }
public object ProvideValue(IMarkupExtensionContext context) { Object realSource = Source ?? context.DataContext; if ( null != realSource && !( realSource is INotifyPropertyChanged ) ) { throw new ArgumentException("Source must be INotifyPropertyChanged to use bindings"); } if (null != realSource) { BindingMode mode = BindingMode.Default; if ( Path != null ) { Type enumType = typeof ( BindingMode ); string[ ] enumNames = enumType.GetEnumNames( ); for ( int i = 0, len = enumNames.Length; i < len; i++ ) { if ( enumNames[ i ] == Mode ) { mode = ( BindingMode ) Enum.ToObject( enumType, enumType.GetEnumValues( ).GetValue( i ) ); break; } } } BindingBase binding = new BindingBase( context.Object, context.PropertyName, (INotifyPropertyChanged) realSource, Path, mode); if ( Converter != null ) binding.Converter = Converter; binding.Bind( ); // mb return actual property value ? return null; } return null; }
public object ProvideValue(IMarkupExtensionContext context) { Object realSource = Source ?? context.DataContext; if (null != realSource && realSource is INotifyPropertyChanged) { BindingMode mode = BindingMode.Default; if (Path != null) { Type enumType = typeof(BindingMode); string[] enumNames = enumType.GetEnumNames( ); for (int i = 0, len = enumNames.Length; i < len; i++) { if (enumNames[i] == Mode) { mode = ( BindingMode )Enum.ToObject(enumType, enumType.GetEnumValues( ).GetValue(i)); break; } } } BindingBase binding = new BindingBase(context.Object, context.PropertyName, (INotifyPropertyChanged)realSource, Path, mode); if (Converter != null) { binding.Converter = Converter; } binding.Bind( ); // mb return actual property value ? return(null); } return(null); }
public object ProvideValue( IMarkupExtensionContext context ) { if (string.IsNullOrEmpty( Ref )) throw new InvalidOperationException("Ref is null or empty string."); object obj = context.GetObjectById( Ref ); if ( null == obj ) { if ( context.IsFixupTokenAvailable ) { return context.GetFixupToken( new string[ ] { Ref } ); } else { throw new InvalidOperationException(string.Format("Object with Id={0} not found.", Ref)); } } return obj; }
public object ProvideValue(IMarkupExtensionContext context) { if (null == Converter) { throw new InvalidOperationException("Converter is null"); } if (null == Value) { return(null); } PropertyInfo propertyInfo = context.Object.GetType( ).GetProperty(context.PropertyName); Type propertyType = propertyInfo.PropertyType; Type valueType = Value.GetType( ); Type firstType = Converter.FirstType; Type secondType = Converter.SecondType; if (firstType.IsAssignableFrom(propertyType) && secondType.IsAssignableFrom(valueType)) { ConversionResult conversionResult = Converter.ConvertBack(Value); if (!conversionResult.Success) { throw new InvalidOperationException(string.Format( "Cannot convert value : {0}", conversionResult.FailReason)); } return(conversionResult.Value); } else if (firstType.IsAssignableFrom(valueType) && secondType.IsAssignableFrom(propertyType)) { ConversionResult conversionResult = Converter.Convert(Value); if (!conversionResult.Success) { throw new InvalidOperationException(string.Format( "Cannot convert value : {0}", conversionResult.FailReason)); } return(conversionResult.Value); } else { throw new InvalidOperationException( string.Format("Cannot use specified converter to convert {0} to {1}", valueType.Name, propertyType.Name)); } }
public Object ProcessMarkupExtension(IMarkupExtensionContext context) { // interpret as markup extension expression object result = processMarkupExtensionCore(context); if (result is IFixupToken) { return(result); } if (hasNextChar( )) { throw new InvalidOperationException( String.Format("Syntax error: unexpected characters at {0}", index)); } return(result); }
public object ProvideValue(IMarkupExtensionContext context) { if (string.IsNullOrEmpty(Ref)) { throw new InvalidOperationException("Ref is null or empty string."); } object obj = context.GetObjectById(Ref); if (null == obj) { if (context.IsFixupTokenAvailable) { return(context.GetFixupToken(new string[] { Ref })); } else { throw new InvalidOperationException(string.Format("Object with Id={0} not found.", Ref)); } } return(obj); }
public object ProvideValue(IMarkupExtensionContext context) { return(Type.GetType(Name)); }
public object ProvideValue( IMarkupExtensionContext context ) { return Type.GetType( Name ); }
public object ProvideValue(IMarkupExtensionContext context) { return Property1 + "_" + Property2 + "_" + Property3; }
/// <summary> /// Recursive method. Consumes next characters as markup extension definition. /// Resolves type, ctor arguments and properties of markup extension, /// constructs and initializes it, and returns ProvideValue method result. /// </summary> /// <param name="context">Context object passed to ProvideValue method.</param> private Object processMarkupExtensionCore(IMarkupExtensionContext context) { if (consumeChar( ) != '{') { throw new InvalidOperationException("Syntax error: '{{' token expected at 0."); } processWhitespace(false); String markupExtensionName = processQualifiedName( ); if (markupExtensionName.Length == 0) { throw new InvalidOperationException("Syntax error: markup extension name is empty."); } processWhitespace( ); Type type = resolver.Resolve(markupExtensionName); Object obj = null; List <Object> ctorArgs = new List <object>(); for ( ;;) { if (peekNextChar( ) == '{') { // inner markup extension processing // syntax error if ctor arg defined after any property if (obj != null) { throw new InvalidOperationException("Syntax error: constructor argument" + " cannot be after property assignment."); } Object value = processMarkupExtensionCore(context); if (value is IFixupToken) { return(value); } ctorArgs.Add(value); } else { String membernameOrString = processString( ); if (membernameOrString.Length == 0) { throw new InvalidOperationException( String.Format("Syntax error: member name or string expected at {0}", index)); } if (peekNextChar( ) == '=') { consumeChar( ); object value = peekNextChar( ) == '{' ? processMarkupExtensionCore(context) : processString( ); if (value is IFixupToken) { return(value); } // construct object if not constructed yet if (obj == null) { obj = construct(type, ctorArgs); } // assign value to specified member assignProperty(type, obj, membernameOrString, value); } else if (peekNextChar( ) == ',' || peekNextChar( ) == '}') { // syntax error if ctor arg defined after any property if (obj != null) { throw new InvalidOperationException("Syntax error: constructor argument" + " cannot be after property assignment."); } // store membernameOrString as string argument of ctor ctorArgs.Add(membernameOrString); } else { // it is '{' token, throw syntax error throw new InvalidOperationException( String.Format("Syntax error : unexpected '{{' token at {0}.", index)); } } // after ctor arg or property assignment should be , or } if (peekNextChar( ) == ',') { consumeChar( ); } else if (peekNextChar( ) == '}') { consumeChar( ); // construct object if (obj == null) { obj = construct(type, ctorArgs); } // markup extension is finished break; } else { // it is '{' token (without whitespace), throw syntax error throw new InvalidOperationException( String.Format("Syntax error : unexpected '{{' token at {0}.", index)); } processWhitespace(false); } return(((IMarkupExtension)obj).ProvideValue(context)); }
/// <summary> /// Recursive method. Consumes next characters as markup extension definition. /// Resolves type, ctor arguments and properties of markup extension, /// constructs and initializes it, and returns ProvideValue method result. /// </summary> /// <param name="context">Context object passed to ProvideValue method.</param> private Object processMarkupExtensionCore( IMarkupExtensionContext context) { if (consumeChar( ) != '{') throw new InvalidOperationException("Syntax error: '{{' token expected at 0."); processWhitespace( false ); String markupExtensionName = processQualifiedName( ); if ( markupExtensionName.Length == 0 ) throw new InvalidOperationException( "Syntax error: markup extension name is empty." ); processWhitespace( ); Type type = resolver.Resolve(markupExtensionName); Object obj = null; List<Object> ctorArgs = new List< object >(); for ( ;; ) { if ( peekNextChar( ) == '{' ) { // inner markup extension processing // syntax error if ctor arg defined after any property if ( obj != null ) throw new InvalidOperationException("Syntax error: constructor argument" + " cannot be after property assignment."); Object value = processMarkupExtensionCore( context ); if ( value is IFixupToken ) return value; ctorArgs.Add( value ); } else { String membernameOrString = processString( ); if (membernameOrString.Length == 0) throw new InvalidOperationException( String.Format("Syntax error: member name or string expected at {0}", index)); if ( peekNextChar( ) == '=' ) { consumeChar( ); object value = peekNextChar( ) == '{' ? processMarkupExtensionCore( context ) : processString( ); if ( value is IFixupToken ) return value; // construct object if not constructed yet if ( obj == null ) obj = construct(type, ctorArgs); // assign value to specified member assignProperty( type, obj, membernameOrString, value ); } else if ( peekNextChar( ) == ',' || peekNextChar( ) == '}' ) { // syntax error if ctor arg defined after any property if (obj != null) throw new InvalidOperationException("Syntax error: constructor argument" + " cannot be after property assignment."); // store membernameOrString as string argument of ctor ctorArgs.Add( membernameOrString ); } else { // it is '{' token, throw syntax error throw new InvalidOperationException( String.Format("Syntax error : unexpected '{{' token at {0}.", index) ); } } // after ctor arg or property assignment should be , or } if ( peekNextChar( ) == ',' ) { consumeChar( ); } else if ( peekNextChar( ) == '}' ) { consumeChar( ); // construct object if ( obj == null ) obj = construct( type, ctorArgs ); // markup extension is finished break; } else { // it is '{' token (without whitespace), throw syntax error throw new InvalidOperationException( String.Format( "Syntax error : unexpected '{{' token at {0}.", index ) ); } processWhitespace( false ); } return ((IMarkupExtension) obj).ProvideValue( context ); }
public Object ProcessMarkupExtension( IMarkupExtensionContext context ) { // interpret as markup extension expression object result = processMarkupExtensionCore(context); if ( result is IFixupToken ) return result; if ( hasNextChar( ) ) { throw new InvalidOperationException( String.Format("Syntax error: unexpected characters at {0}", index)); } return result; }