// Copyright (c) 2019 AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows; using ICSharpCode.WpfDesign.Interfaces; namespace ICSharpCode.WpfDesign { /// /// Represents a property of a DesignItem. /// All changes done via the DesignItemProperty class are represented in the underlying model (e.g. XAML). /// All such changes are recorded in the currently running designer transaction (), /// enabling Undo/Redo support. /// Warning: Changes directly done to control instances might not be reflected in the model. /// public abstract class DesignItemProperty : INotifyPropertyChanged { /// /// Gets the property name. /// public abstract string Name { get; } /// /// Gets the return type of the property. /// public abstract Type ReturnType { get; } /// /// Gets the type that declares the property. /// public abstract Type DeclaringType { get; } /// /// Gets the category of the property. /// public abstract string Category { get; } /// /// Gets the type converter used to convert property values to/from string. /// public virtual TypeConverter TypeConverter { get { return TypeDescriptor.GetConverter(this.ReturnType); } } /// /// Gets if the property represents a collection. /// public abstract bool IsCollection { get; } /// /// Gets if the property represents an event. /// public abstract bool IsEvent { get; } /// /// Gets the elements represented by the collection. /// public abstract IObservableList CollectionElements { get; } /// /// Gets the value of the property. This property returns null if the value is not set, /// or if the value is set to a primitive value. /// public abstract DesignItem Value { get; } /// /// Gets the string value of the property. This property returns null if the value is not set, /// or if the value is set to a non-primitive value (i.e. represented by a , accessible through property). /// public abstract string TextValue { get; } /// /// Is raised when the value of the property changes (by calling or ). /// public abstract event EventHandler ValueChanged; /// /// Is raised when the property changes. /// This event is not raised when the value is changed without going through the designer infrastructure. /// public abstract event EventHandler ValueOnInstanceChanged; /// /// Gets/Sets the value of the property on the designed instance. /// If the property is not set, this returns the default value. /// public abstract object ValueOnInstance { get; } /// /// Gets the value of the property on the designed instance. /// If the property is not set, this returns the default value. /// public abstract object DesignerValue { get; } /// /// Sets the value of the property. /// public abstract void SetValue(object value); /// /// Gets if the property is set on the design item. /// public abstract bool IsSet { get; } /// /// Occurs when the value of the IsSet property changes. /// public abstract event EventHandler IsSetChanged; /// /// Resets the property value to the default, possibly removing it from the list of properties. /// public abstract void Reset(); /// /// Gets the value of as an instance of T (converted using if required and the converter is capable). /// If the property is not set, or does not match type T, this returns the default value. /// public abstract T GetConvertedValueOnInstance(); /// /// Gets the parent design item. /// public abstract DesignItem DesignItem { get; } /// /// Gets the dependency property, or null if this property does not represent a dependency property. /// public abstract DependencyProperty DependencyProperty { get; } /// /// Gets if this property is considered "advanced" and should be hidden by default in a property grid. /// public virtual bool IsAdvanced { get { return false; } } /// /// Gets the full name of the property (DeclaringType.FullName + "." + Name). /// public string FullName { get { return DeclaringType.FullName + "." + Name; } } /// /// Gets the View of the Value or the ValueOnInstance /// (e.g. a Content Property has a DesignItem if it's a Complex Object, if it's only a Text it only has ValueOnInstance) /// public object ValueOnInstanceOrView { get { return Value == null ? ValueOnInstance : Value.View; } } /// /// Gets the full name of the dependency property. Returns the normal FullName if the property /// isn't a dependency property. /// public string DependencyFullName { get { if (DependencyProperty != null) { return DependencyProperty.GetFullName(); } return FullName; } } /// /// It's raised when a property value changes. /// public event PropertyChangedEventHandler PropertyChanged; /// /// Raises the Property changed Event for the speciefied Property /// /// protected virtual void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } } /// /// Represents a collection of design item properties. /// public abstract class DesignItemPropertyCollection : IEnumerable { /// /// Gets the property with the specified name. /// public DesignItemProperty this[string name] { get { return GetProperty(name); } } /// /// Gets the design item property representing the specified dependency property. /// The property must not be an attached property. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1043")] public DesignItemProperty this[DependencyProperty dependencyProperty] { get { return GetProperty(dependencyProperty); } } /// /// Gets the design item property with the specified name. /// public abstract DesignItemProperty GetProperty(string name); /// /// Gets the attached property on the specified owner with the specified name. /// public abstract DesignItemProperty GetAttachedProperty(Type ownerType, string name); /// /// Gets the design item property representing the specified dependency property. /// The property must not be an attached property. /// public DesignItemProperty GetProperty(DependencyProperty dependencyProperty) { if (dependencyProperty == null) throw new ArgumentNullException("dependencyProperty"); return GetProperty(dependencyProperty.Name); } /// /// Gets the design item property representing the specified attached dependency property. /// public DesignItemProperty GetAttachedProperty(DependencyProperty dependencyProperty) { if (dependencyProperty == null) throw new ArgumentNullException("dependencyProperty"); return GetAttachedProperty(dependencyProperty.OwnerType, dependencyProperty.Name); } /// /// Gets an enumerator to enumerate the properties that have a non-default value. /// public abstract IEnumerator GetEnumerator(); System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } } }