代码之家  ›  专栏  ›  技术社区  ›  akjoshi HCP

WPF PropertyGrid-添加对集合的支持

  •  2
  • akjoshi HCP  · 技术社区  · 16 年前

    PropertyGrid IList ObservableCollection 等)属性。我有点搞不懂如何跟踪(该集合的)所选项目并将其传递给客户。

    如果解决方案使用开源WPF属性grid( http://www.codeplex.com/wpg )我将把更改/添加实施回控件中。

    1 回复  |  直到 14 年前
        1
  •  2
  •   akjoshi HCP    11 年前

    没有答案证明没有直截了当的方法。所以我用这种方式实现了这个特性-

    我创建了一个名为 RelatedItemSourcePropertyAttribute 像这样-

    /// <summary>
    /// Attribute to identify the related item source property.
    /// Note: Property should be of IEnumerable type
    /// </summary>
    [global::System.AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
    public sealed class RelatedItemSourcePropertyAttribute : Attribute
    {
        // See the attribute guidelines at 
        //  http://go.microsoft.com/fwlink/?LinkId=85236
    
        private string relatedPropertyName;
        public static readonly RelatedItemSourcePropertyAttribute Default = new RelatedItemSourcePropertyAttribute(string.Empty);
    
        /// <summary>
        /// Initializes a new instance of the <see cref="RelatedPropertyAttribute"/> class.
        /// </summary>
        /// <param name="relatedPropertyName">Name of the related property.</param>
        public RelatedItemSourcePropertyAttribute(string relatedPropertyName)
        {
            this.relatedPropertyName = relatedPropertyName;
        }
    
        /// <summary>
        /// Gets a value indicating whether [related property name].
        /// </summary>
        /// <value><c>true</c> if [related property name]; otherwise, <c>false</c>.</value>
        public string RelatedPropertyName
        {
            get { return relatedPropertyName; }
        }
    
        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        ///     <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public override bool Equals(object obj)
        {
            if (!(obj is RelatedItemSourcePropertyAttribute))
                return false;
            if (obj == this)
                return true;
            return ((RelatedItemSourcePropertyAttribute)obj).relatedPropertyName == relatedPropertyName;
        }
    
        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public override int GetHashCode()
        {
            return relatedPropertyName.GetHashCode();
        }
    
        /// <summary>
        /// When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.
        /// </summary>
        /// <returns>
        /// true if this instance is the default attribute for the class; otherwise, false.
        /// </returns>
        public override bool IsDefaultAttribute()
        {
            return relatedPropertyName == RelatedItemSourcePropertyAttribute.Default.relatedPropertyName;
        }
    }
    

    此属性将采用相关项源属性的属性名(其值将用于填充下拉列表)。它将被这样使用-

        [RelatedItemSourceProperty("UnitNames")]
        public virtual string SelectedUnit
        {
            get { return (string)GetValue(SelectedUnitProperty); }
            set { SetValue(SelectedUnitProperty, value); }
        }
        public static readonly DependencyProperty SelectedUnitProperty =
            DependencyProperty.Register("SelectedUnit", typeof(string), typeof(BaseControl),
            new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(SelectedUnitChangedCallBack)));
    
    
        public virtual ObservableCollection<string> UnitNames
        {
            get { return (ObservableCollection<string>)GetValue(UnitNamesProperty); }
            set { SetValue(UnitNamesProperty, value); }
        }
        public static readonly DependencyProperty UnitNamesProperty =
            DependencyProperty.Register("UnitNames", typeof(ObservableCollection<string>),
            typeof(BaseProperties), new PropertyMetadata(null)); //Validation
    

    然后在属性中,我用组合框绑定了相关的item源属性。

    希望能找到比这更好的解决方案:)