WpfDesigner/WpfDesign/Project/DesignItemProperty.cs

275 строки
9.1 KiB
C#
Исходник Постоянная ссылка Обычный вид История

2019-03-22 02:22:28 +03:00
// 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.
2015-10-10 14:44:48 +03:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
2015-10-22 17:49:52 +03:00
using ICSharpCode.WpfDesign.Interfaces;
2015-10-10 14:44:48 +03:00
namespace ICSharpCode.WpfDesign
2019-03-22 02:22:28 +03:00
{
2015-10-10 14:44:48 +03:00
/// <summary>
/// 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 (<see cref="ChangeGroup"/>),
/// enabling Undo/Redo support.
/// Warning: Changes directly done to control instances might not be reflected in the model.
/// </summary>
2015-10-22 17:57:28 +03:00
public abstract class DesignItemProperty : INotifyPropertyChanged
2019-03-22 02:22:28 +03:00
{
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the property name.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract string Name { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the return type of the property.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract Type ReturnType { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the type that declares the property.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract Type DeclaringType { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the category of the property.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract string Category { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the type converter used to convert property values to/from string.
/// </summary>
2019-03-22 02:22:28 +03:00
public virtual TypeConverter TypeConverter
{
2015-10-10 14:44:48 +03:00
get { return TypeDescriptor.GetConverter(this.ReturnType); }
2019-03-22 02:22:28 +03:00
}
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets if the property represents a collection.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract bool IsCollection { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets if the property represents an event.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract bool IsEvent { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the elements represented by the collection.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract IObservableList<DesignItem> CollectionElements { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// 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.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract DesignItem Value { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// 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 <see cref="DesignItem"/>, accessible through <see cref="Value"/> property).
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract string TextValue { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Is raised when the value of the property changes (by calling <see cref="SetValue"/> or <see cref="Reset"/>).
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract event EventHandler ValueChanged;
2015-10-10 14:44:48 +03:00
/// <summary>
/// Is raised when the <see cref="ValueOnInstance"/> property changes.
/// This event is not raised when the value is changed without going through the designer infrastructure.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract event EventHandler ValueOnInstanceChanged;
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets/Sets the value of the property on the designed instance.
/// If the property is not set, this returns the default value.
/// </summary>
public abstract object ValueOnInstance { get; }
/// <summary>
/// Gets the value of the property on the designed instance.
/// If the property is not set, this returns the default value.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract object DesignerValue { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Sets the value of the property.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract void SetValue(object value);
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets if the property is set on the design item.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract bool IsSet { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Occurs when the value of the IsSet property changes.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract event EventHandler IsSetChanged;
2015-10-10 14:44:48 +03:00
/// <summary>
/// Resets the property value to the default, possibly removing it from the list of properties.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract void Reset();
/// <summary>
/// Gets the value of <see cref="ValueOnInstance"/> as an instance of T (converted using <see cref="TypeConverter"/> if required and the converter is capable).
/// If the property is not set, or does not match type T, this returns the default value.
/// </summary>
public abstract T GetConvertedValueOnInstance<T>();
2019-03-22 02:22:28 +03:00
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the parent design item.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract DesignItem DesignItem { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the dependency property, or null if this property does not represent a dependency property.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract DependencyProperty DependencyProperty { get; }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets if this property is considered "advanced" and should be hidden by default in a property grid.
/// </summary>
2019-03-22 02:22:28 +03:00
public virtual bool IsAdvanced { get { return false; } }
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the full name of the property (DeclaringType.FullName + "." + Name).
/// </summary>
2019-03-22 02:22:28 +03:00
public string FullName
{
2019-03-22 02:22:28 +03:00
get
{
2015-10-10 14:44:48 +03:00
return DeclaringType.FullName + "." + Name;
}
2019-03-22 02:22:28 +03:00
}
2015-10-22 17:57:28 +03:00
/// <summary>
/// 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)
/// </summary>
2019-03-22 02:22:28 +03:00
public object ValueOnInstanceOrView
{
get { return Value == null ? ValueOnInstance : Value.View; }
}
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the full name of the dependency property. Returns the normal FullName if the property
/// isn't a dependency property.
/// </summary>
2019-03-22 02:22:28 +03:00
public string DependencyFullName
{
2019-03-22 02:22:28 +03:00
get
{
2019-03-22 02:22:28 +03:00
if (DependencyProperty != null)
{
2015-10-10 14:44:48 +03:00
return DependencyProperty.GetFullName();
}
return FullName;
}
2019-03-22 02:22:28 +03:00
}
/// <summary>
/// It's raised when a property value changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the Property changed Event for the speciefied Property
/// </summary>
2015-12-23 14:03:50 +03:00
/// <param name="propertyName"></param>
2019-03-22 02:22:28 +03:00
protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
2019-03-22 02:22:28 +03:00
}
2015-10-10 14:44:48 +03:00
/// <summary>
/// Represents a collection of design item properties.
/// </summary>
public abstract class DesignItemPropertyCollection : IEnumerable<DesignItemProperty>
2019-03-22 02:22:28 +03:00
{
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the property with the specified name.
/// </summary>
2019-03-22 02:22:28 +03:00
public DesignItemProperty this[string name]
{
2015-10-10 14:44:48 +03:00
get { return GetProperty(name); }
2019-03-22 02:22:28 +03:00
}
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the design item property representing the specified dependency property.
/// The property must not be an attached property.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1043")]
2019-03-22 02:22:28 +03:00
public DesignItemProperty this[DependencyProperty dependencyProperty]
{
2019-03-22 02:22:28 +03:00
get
{
2015-10-10 14:44:48 +03:00
return GetProperty(dependencyProperty);
}
2019-03-22 02:22:28 +03:00
}
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the design item property with the specified name.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract DesignItemProperty GetProperty(string name);
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the attached property on the specified owner with the specified name.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract DesignItemProperty GetAttachedProperty(Type ownerType, string name);
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the design item property representing the specified dependency property.
/// The property must not be an attached property.
/// </summary>
public DesignItemProperty GetProperty(DependencyProperty dependencyProperty)
{
if (dependencyProperty == null)
throw new ArgumentNullException("dependencyProperty");
return GetProperty(dependencyProperty.Name);
2019-03-22 02:22:28 +03:00
}
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets the design item property representing the specified attached dependency property.
/// </summary>
public DesignItemProperty GetAttachedProperty(DependencyProperty dependencyProperty)
{
if (dependencyProperty == null)
throw new ArgumentNullException("dependencyProperty");
return GetAttachedProperty(dependencyProperty.OwnerType, dependencyProperty.Name);
2019-03-22 02:22:28 +03:00
}
2015-10-10 14:44:48 +03:00
/// <summary>
/// Gets an enumerator to enumerate the properties that have a non-default value.
/// </summary>
2019-03-22 02:22:28 +03:00
public abstract IEnumerator<DesignItemProperty> GetEnumerator();
2015-10-10 14:44:48 +03:00
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}