【问题标题】:WPF PropertyGrid - adding support for collectionsWPF PropertyGrid - 添加对集合的支持
【发布时间】:2011-02-18 11:33:58
【问题描述】:

我正在开发 wpf PropertyGrid(PG) 控件,我希望 PG 支持集合类型(IListObservableCollection 等)属性。我对如何跟踪(该集合的)选定项目并将其传递给客户端感到有些困惑。

有什么想法吗?

如果解决方案使用开源 WPF PropertyGrid (http://www.codeplex.com/wpg),我会将更改/添加重新实施到控件中。

【问题讨论】:

    标签: .net wpf silverlight collections propertygrid


    【解决方案1】:

    没有答案证明没有直接的方法可以做到这一点。所以我以这种方式实现了这个功能-

    我像这样创建了一个名为 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
    

    然后在属性中我将相关的项目源属性与组合框绑定。

    希望看到比这个更好的解决方案:)

    【讨论】:

    • 我也有同样的情况。我需要向 WPG 添加收藏支持。我实现了这段代码;但我认为还不够。
    • @Bahadir arslan:您能否提供有关您所面临的确切问题/错误的更多详细信息?我已经在我的应用程序中实现了同样的功能并且工作正常。
    • 我需要将属性显示为列表。例如,用户可以在城市列表中选择城市。所以我找到了你的帖子;但我无法实现:) 当我实现您的解决方案时,首先依赖属性在属性网格上变得可见,其次我的属性(SelectedUnitProperty)显示为文本框。之后我找到了 WPGTemplates.xaml 并编辑了 IList 类型模板;所以我可以将我的列表显示为组合框:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    相关资源
    最近更新 更多