Example #1
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (!(destinationType == typeof(InstanceDescriptor)))
            {
                return(base.ConvertTo(context, culture, value, destinationType));
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            TemplateBindingExtension templateBindingExtension = value as TemplateBindingExtension;

            if (templateBindingExtension == null)
            {
                throw new ArgumentException(SR.Get("MustBeOfType", new object[]
                {
                    "value",
                    "TemplateBindingExtension"
                }), "value");
            }
            return(new InstanceDescriptor(typeof(TemplateBindingExtension).GetConstructor(new Type[]
            {
                typeof(DependencyProperty)
            }), new object[]
            {
                templateBindingExtension.Property
            }));
        }
Example #2
0
		private void setColor(Button button, MetrialColor.Name color_name)
		{
			Style style = new Style();

			style.Setters.Add(new Setter(BackgroundProperty, MetrialColor.getBrush(color_name, 3)));
			style.Setters.Add(new Setter(ForegroundProperty, MetrialColor.getBrush(MetrialColor.Name.White)));

			Trigger button_pressed_trigger = new Trigger();
			//mouse_over_trigger.Property = UIElement.IsMouseOverProperty;
			button_pressed_trigger.Property = Button.IsPressedProperty;
			button_pressed_trigger.Value = true;
			button_pressed_trigger.Setters.Add(new Setter(BackgroundProperty, MetrialColor.getBrush(color_name, 4)));
			button_pressed_trigger.Setters.Add(new Setter(BorderThicknessProperty, new Thickness(5)));
			button_pressed_trigger.Setters.Add(new Setter(BorderBrushProperty, MetrialColor.getBrush(color_name, 1)));
			style.Triggers.Add(button_pressed_trigger);

			ControlTemplate control_template = new ControlTemplate(typeof(Button));
			var border = new FrameworkElementFactory(typeof(Border));
			border.Name = "button_border";
			border.SetValue(BorderThicknessProperty, new Thickness(0));
			border.SetValue(BorderBrushProperty, MetrialColor.getBrush(color_name, 3));
			var binding = new TemplateBindingExtension();
			binding.Property = BackgroundProperty;
			border.SetValue(BackgroundProperty, binding);

			/* border color change on mouse pressed
			Trigger mouse_over_trigger_border = new Trigger();
			mouse_over_trigger_border.Property = Button.IsPressedProperty;
			mouse_over_trigger_border.Value = true;
			mouse_over_trigger_border.Setters.Add(new Setter(BorderBrushProperty, MetrialColor.getBrush(mItem.getColorName(), 3), "button_border"));
			control_template.Triggers.Add(mouse_over_trigger_border);
			*/

			FrameworkElementFactory content_presenter = new FrameworkElementFactory(typeof(ContentPresenter));
			content_presenter.SetValue(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center);
			content_presenter.SetValue(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
			border.AppendChild(content_presenter);

			control_template.VisualTree = border;

			style.Setters.Add(new Setter(TemplateProperty, control_template));

			button.Style = style;
		}
Example #3
0
 internal TemplateBindingExpression(TemplateBindingExtension templateBindingExtension)
 {
     _templateBindingExtension = templateBindingExtension;
 }
 internal TemplateBindingExpression( TemplateBindingExtension templateBindingExtension )
 {
     _templateBindingExtension = templateBindingExtension;
 }
Example #5
0
        // The end of the constructor parameter section has been reached.  Create an
        // instance of the object after finding the appropriate constructor and converting 
        // all of the objects held on the stack. 
        internal virtual void ReadConstructorParametersEndRecord()
        { 
            Type   elementType = ParentContext.ExpectedType;
            short  positiveElementTypeId = (short)-ParentContext.ExpectedTypeId;

            object param = null; 
            ArrayList paramList = null;
            int paramCount; 
            object instance = null; 
            bool foundInstance = false;
 
            if( TraceMarkup.IsEnabled )
            {
                TraceMarkup.Trace( TraceEventType.Start,
                                 TraceMarkup.CreateMarkupExtension, 
                                 elementType );
            } 
 
            if (CurrentContext.CheckFlag(ReaderFlags.SingletonConstructorParam))
            { 
                param = CurrentContext.ObjectData;
                paramCount = 1;

                // Fast code path for [static/dynamic] resource extensions & 
                // Type/Static/TemplateBinding extensions
                switch (positiveElementTypeId) 
                { 
                    case (short)KnownElements.TypeExtension:
 
                        // Note that this assumes that TypeExtension has a
                        // constructor with one param of type Type or String.
                        Type t = param as Type;
                        if (t != null) 
                        {
                            instance = new TypeExtension(t); 
                        } 
                        else
                        { 
                            Debug.Assert(param is String);
                            instance = new TypeExtension((String)param);
                        }
 
                        foundInstance = true;
                        break; 
 
                    case (short)KnownElements.StaticResourceExtension:
 
                        // Note that this assumes that StaticResourceExtension has a
                        // constructor with one param of type object.
                        instance = new StaticResourceExtension(param);
                        foundInstance = true; 
                        break;
 
                    case (short)KnownElements.DynamicResourceExtension: 

                        // Note that this assumes that DynamicResourceExtension has a 
                        // constructor with one param of type object.
                        instance = new DynamicResourceExtension(param);
                        foundInstance = true;
                        break; 

                    case (short)KnownElements.StaticExtension: 
 
                        // Note that this assumes that StaticExtension has a default
                        // constructor and one public property of type string and one 
                        // internal property of type object for optimized member info.
                        instance = new StaticExtension((string)param);
                        foundInstance = true;
                        break; 

                    case (short)KnownElements.TemplateBindingExtension: 
 
                        // Note that this assumes that TemplateBindingExtension has a
                        // constructor with one param of type DependencyProperty. If a 
                        // string is passed in due to there being other attributes like
                        // converter being set, then that needs to be converted now first.
                        DependencyProperty dp = param as DependencyProperty;
                        if (dp == null) 
                        {
                            string paramString = param as string; 
                            Type ownerType = ParserContext.TargetType; 
                            Debug.Assert(paramString != null);
 
                            dp = XamlTypeMapper.ParsePropertyName(ParserContext,
                                                                  paramString.Trim(),
                                                                  ref ownerType);
 
                            if (dp == null)
                            { 
                                ThrowException(SRID.ParserNoDPOnOwner, paramString, ownerType.FullName); 
                            }
                        } 

                        instance = new TemplateBindingExtension(dp);
                        foundInstance = true;
                        break; 
                }
            } 
            else 
            {
                paramList = (ArrayList)CurrentContext.ObjectData; 
                paramCount = paramList.Count;
            }

            if (!foundInstance) 
            {
                // Find the constructor based on the number of parameters stored in paramList 
                XamlTypeMapper.ConstructorData data = XamlTypeMapper.GetConstructors(elementType); 
                ConstructorInfo[] infos = data.Constructors;
                for (int i=0; i<infos.Length; i++) 
                {
                    ConstructorInfo info = infos[i];
                    ParameterInfo[] paramInfos = data.GetParameters(i);
                    if (paramInfos.Length == paramCount) 
                    {
                        object[] paramArray = new object[paramInfos.Length]; 
 
                        if (paramCount == 1)
                        { 
                            Debug.Assert(param != null && paramList == null, "Must have a single param");
                            ProcessConstructorParameter(paramInfos[0], param, ref paramArray[0]);

                            // Fast code path for other markupextensions 
                            if (positiveElementTypeId == (short)KnownElements.RelativeSource)
                            { 
                                // Note that this assumes that RelativeSource has a 
                                // constructor with one param of type RelativeSourceMode.
                                instance = new System.Windows.Data.RelativeSource((System.Windows.Data.RelativeSourceMode)paramArray[0]); 
                                foundInstance = true;
                            }
                        }
                        else 
                        {
                            Debug.Assert(param == null && paramList != null, "Must have a paramList"); 
 
                            // Check each type and attempt to convert the paramList using
                            // the type converter associated with each parameter type. 
                            for (int j=0; j<paramInfos.Length; j++)
                            {
                                ProcessConstructorParameter(paramInfos[j], paramList[j], ref paramArray[j]);
                            } 
                        }
 
                        if (!foundInstance) 
                        {
                            // If we make it to here we have a list of converted parameters, so 
                            // invoke the constructor with that list.
#if !STRESS
                            try
                            { 
#endif
                                instance = info.Invoke(paramArray); 
                                foundInstance = true; 
#if !STRESS
                            } 
                            catch (Exception e)
                            {
                                if (CriticalExceptions.IsCriticalException(e) || e is XamlParseException)
                                { 
                                    throw;
                                } 
 
                                TargetInvocationException tie = e as TargetInvocationException;
                                if( tie != null ) 
                                {
                                    e = tie.InnerException;
                                }
 
                                ThrowExceptionWithLine(SR.Get(SRID.ParserFailedToCreateFromConstructor, info.DeclaringType.Name),  e);
 
                            } 
#endif
                        } 
                    }
                }
            }
 
            if (foundInstance)
            { 
                ParentContext.ObjectData = instance; 
                ParentContext.ExpectedType = null;
                PopContext(); 
            }
            else
            {
                // If we get to here, then no matching constructor was found, so complain 
                ThrowException(SRID.ParserBadConstructorParams, elementType.Name, paramCount.ToString(CultureInfo.CurrentCulture));
            } 
 
            if( TraceMarkup.IsEnabled )
            { 
                TraceMarkup.Trace( TraceEventType.Stop,
                                 TraceMarkup.CreateMarkupExtension,
                                 elementType,
                                 instance ); 
            }
 
        } 
        /// <summary>Sets the value of a dependency property.</summary>
        /// <param name="dp">The dependency property identifier of the property to set.</param>
        /// <param name="value">The new value.</param>
        // Token: 0x0600067D RID: 1661 RVA: 0x00014260 File Offset: 0x00012460
        public void SetValue(DependencyProperty dp, object value)
        {
            if (this._sealed)
            {
                throw new InvalidOperationException(SR.Get("CannotChangeAfterSealed", new object[]
                {
                    "FrameworkElementFactory"
                }));
            }
            if (dp == null)
            {
                throw new ArgumentNullException("dp");
            }
            if (!dp.IsValidValue(value) && !(value is MarkupExtension) && !(value is DeferredReference))
            {
                throw new ArgumentException(SR.Get("InvalidPropertyValue", new object[]
                {
                    value,
                    dp.Name
                }));
            }
            if (StyleHelper.IsStylingLogicalTree(dp, value))
            {
                throw new NotSupportedException(SR.Get("ModifyingLogicalTreeViaStylesNotImplemented", new object[]
                {
                    value,
                    "FrameworkElementFactory.SetValue"
                }));
            }
            if (dp.ReadOnly)
            {
                throw new ArgumentException(SR.Get("ReadOnlyPropertyNotAllowed", new object[]
                {
                    dp.Name,
                    base.GetType().Name
                }));
            }
            ResourceReferenceExpression resourceReferenceExpression = value as ResourceReferenceExpression;
            DynamicResourceExtension    dynamicResourceExtension    = value as DynamicResourceExtension;
            object obj = null;

            if (resourceReferenceExpression != null)
            {
                obj = resourceReferenceExpression.ResourceKey;
            }
            else if (dynamicResourceExtension != null)
            {
                obj = dynamicResourceExtension.ResourceKey;
            }
            if (obj != null)
            {
                this.UpdatePropertyValueList(dp, PropertyValueType.Resource, obj);
                return;
            }
            TemplateBindingExtension templateBindingExtension = value as TemplateBindingExtension;

            if (templateBindingExtension == null)
            {
                this.UpdatePropertyValueList(dp, PropertyValueType.Set, value);
                return;
            }
            this.UpdatePropertyValueList(dp, PropertyValueType.TemplateBinding, templateBindingExtension);
        }