Ejemplo n.º 1
0
        /// <summary>
        /// Returns the default property for this instance of a component.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.ComponentModel.PropertyDescriptor"/> that represents the default property for this object, or null if this object does not have properties.
        /// </returns>
        PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
        {
            PropertySpecification propertySpec = this.PropertySpecifications.FirstOrDefault(p => p.Name == this.DefaultProperty);

            return(propertySpec == null ? null : new PropertySpecificationDescriptor(propertySpec, this,
                                                                                     new Attribute[] { new MergablePropertyAttribute(true) }));
        }
        /// <summary>
        /// Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.
        /// </summary>
        /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context that can be used to extract additional information about the environment from which this converter is invoked. This parameter or properties of this parameter can be null.</param>
        /// <returns>
        /// A <see cref="T:System.ComponentModel.TypeConverter.StandardValuesCollection"/> that holds a standard set of valid values, or null if the data type does not support a standard set of values.
        /// </returns>
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if ((context == null) || (context.PropertyDescriptor == null) || (context.PropertyDescriptor.ComponentType == null) ||
                (context.PropertyDescriptor.ComponentType != typeof(PropertySpecification)))
            {
                return(base.GetStandardValues(context));
            }
            PropertySpecificationDescriptor specificationDescriptor = context.PropertyDescriptor as PropertySpecificationDescriptor;

            if (specificationDescriptor == null)
            {
                // check if the property descriptor is a multi-property descriptor, which is created by various property grids when multiple objects are
                // merged and displayed simultaneously.
                var itemProperty = context.PropertyDescriptor.GetType().GetProperty("Item");
                if (itemProperty == null)
                {
                    return(base.GetStandardValues(context));
                }
                try
                {
                    specificationDescriptor = itemProperty.GetValue(context.PropertyDescriptor, new object[] { 0 }) as PropertySpecificationDescriptor;
                    if (specificationDescriptor == null)
                    {
                        // give up
                        return(base.GetStandardValues(context));
                    }
                }
                catch
                {
                    // no descriptor at index 0
                    return(base.GetStandardValues(context));
                }
            }
            PropertySpecification specification = specificationDescriptor.Specification;

            if ((specification == null) || (specification.ValueList == null) || (specification.ValueList.Count <= 0))
            {
                return(base.GetStandardValues(context));
            }
            return(new StandardValuesCollection(specification.ValueList));
        }
Ejemplo n.º 3
0
		public void AdvancedPropertyDescriptorConstructionTestWithEvents()
		{
			PropertyBag bag = new PropertyBag();
			// add some property specifications. 
			var property1 = new PropertySpecification("Property 1", typeof(string), "Cat1", "Prop1 desc", "Foo");
			// make readonly
			property1.Attributes.Add(ReadOnlyAttribute.Yes);
			bag.PropertySpecifications.Add(property1);

			// add expanding property
			var property2 = new PropertySpecification("Picture", typeof(Image), "Some Category", "This is a sample description.");
			property2.Attributes.Add(new TypeConverterAttribute(typeof(ExpandableObjectConverter)));
			bag.PropertySpecifications.Add(property2);

			// custom editor
			var property3 = new PropertySpecification("Source folder", typeof(string), "OutputFolders", "The output folder for the sourcecode", "c:\\temp");
			property3.Attributes.Add(new EditorAttribute(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)));
			bag.PropertySpecifications.Add(property3);

			// use value list
			var property4 = new PropertySpecification("PickAValue", typeof(string), "Cat1", "A property which value has to be picked from a list");
			property4.TypeConverterType = typeof(PropertySpecificationValuesListTypeConverter);
			property4.ValueList.AddRange(new[] { "One", "Two", "Three", "Many" });
			bag.PropertySpecifications.Add(property4);

			// value list to store values in
			Dictionary<string, object> values = new Dictionary<string, object>();

			// event handler binding so values get store inside the dictionary and also retrieved from it. 
			bag.GetValue += (sender, e) => { e.Value = values.GetValue(e.Property.Name); };
			bag.SetValue += (sender, e) => { values[e.Property.Name] = e.Value; };
			
			// open a testform which binds the bag to the propertygrid. Editing values will store the values in the dictionary, default values are
			// not in the dictionary.
			using(TestForm f = new TestForm(bag))
			{
				f.ShowDialog();
			}
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertySpecificationDescriptor"/> class.
 /// </summary>
 /// <param name="specification">The specification.</param>
 /// <param name="containingBag">The containing bag.</param>
 /// <param name="propertyAttributes">The property attributes.</param>
 public PropertySpecificationDescriptor(PropertySpecification specification, PropertyBag containingBag, Attribute[] propertyAttributes)
     : base(specification.Name, propertyAttributes)
 {
     _containingBag = containingBag;
     _specification = specification;
 }
		/// <summary>
		/// Initializes a new instance of the PropertySpecEventArgs class.
		/// </summary>
		/// <param name="property">The PropertySpec that represents the property whose value is being requested or set.</param>
		/// <param name="val">The current value of the property.</param>
		public PropertySpecificationEventArgs(PropertySpecification property, object val)
		{
			ArgumentVerifier.CantBeNull(property, "property");
			this.Property = property;
			this.Value = val;
		}
 /// <summary>
 /// Initializes a new instance of the PropertySpecEventArgs class.
 /// </summary>
 /// <param name="property">The PropertySpec that represents the property whose value is being requested or set.</param>
 /// <param name="val">The current value of the property.</param>
 public PropertySpecificationEventArgs(PropertySpecification property, object val)
 {
     ArgumentVerifier.CantBeNull(property, "property");
     this.Property = property;
     this.Value    = val;
 }
Ejemplo n.º 7
0
		public void AdvancedPropertyDescriptorConstructionTestWithFuncs()
		{
			PropertyBag bag = new PropertyBag();
			// add some property specifications. 
			var property1 = new PropertySpecification("Property 1", typeof(string), "Cat1", "Prop1 desc", "Foo");
			// make readonly
			property1.Attributes.Add(ReadOnlyAttribute.Yes);
			bag.PropertySpecifications.Add(property1);

			// add expanding property
			var property2 = new PropertySpecification("Picture", typeof(Image), "Some Category", "This is a sample description.");
			property2.Attributes.Add(new TypeConverterAttribute(typeof(ExpandableObjectConverter)));
			bag.PropertySpecifications.Add(property2);

			// custom editor
			var property3 = new PropertySpecification("Source folder", typeof(string), "OutputFolders", "The output folder for the sourcecode", "c:\\temp");
			property3.Attributes.Add(new EditorAttribute(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)));
			property3.ConvertEmptyStringToNull = true;
			bag.PropertySpecifications.Add(property3);

			// use value list
			var property4 = new PropertySpecification("PickAValue", typeof(string), "Cat1", "A property which value has to be picked from a list", "One");
			property4.TypeConverterType = typeof(PropertySpecificationValuesListTypeConverter);
			property4.ValueList.AddRange(new[] { "One", "Two", "Three", "Many" });
			bag.PropertySpecifications.Add(property4);

			var property5 = new PropertySpecification("EnumValue", typeof(ConsoleColor), "Cat1", "Enum property to see that values are converted back/forth to int");
			bag.PropertySpecifications.Add(property5);

			// value list to store values in
			Dictionary<string, object> values = new Dictionary<string, object>();

			// func setting so values get store inside the dictionary and also retrieved from it. Console write added to see what happens when you bind
			// a windows forms propertygrid to a bag: many many redundant calls to the getters/setters occur.
			bag.ValueGetterFunc = (s) =>
									{
										var v = values.GetValue(s);
										Console.WriteLine("Get: {0} : {1}", s, v ?? "<null>");
										return v;
									};
			bag.ValueSetterFunc = (s, v) =>
									{
										values[s] = v;
										Console.WriteLine("Set: {0} : {1}", s, v ?? "<null>");
									};

			// open a testform which binds the bag to the propertygrid. Editing values will store the values in the dictionary, default values are
			// not in the dictionary.
			using(TestForm f = new TestForm(bag))
			{
				f.ShowDialog();
			}
		}
 /// <summary>
 /// Initializes a new instance of the <see cref="PropertySpecificationDescriptor"/> class.
 /// </summary>
 /// <param name="specification">The specification.</param>
 /// <param name="containingBag">The containing bag.</param>
 /// <param name="propertyAttributes">The property attributes.</param>
 public PropertySpecificationDescriptor(PropertySpecification specification, PropertyBag containingBag, Attribute[] propertyAttributes) :
     base(specification.Name, propertyAttributes)
 {
     _containingBag = containingBag;
     _specification = specification;
 }