WPF PropertyGrid中 - 增加了对集合增加了、WPF、PropertyGrid

2023-09-04 00:35:27 作者:元气小少女

我工作的WPF 的PropertyGrid (PG)的控制,我想的PG支持集合类型(的IList 的ObservableCollection 等)的性能。我如何保持选定的项目(即集合)的轨道,并传递到客户端有点糊涂了。

I am working on wpf PropertyGrid(PG) control and I want the PG to support collection type(IList, ObservableCollection etc.) properties. I am bit confused on how to keep track of selected item(of that collection) and pass that to client.

任何想法?

如果该解决方案使用了开源的WPF的PropertyGrid(HTTP://www.$c$cplex.com/wpg)的。我会实现修改/添加回控制

If the solution makes use of the Open Source WPF PropertyGrid (http://www.codeplex.com/wpg) I will implement the changes /additions back into the control.

推荐答案

没有答案,证明有这样做的没有直接的方法。所以,我实现了这个功能,这样一来 -

No answers proves that there is no straight forward way of doing this. So I implemented this feature this way -

我创建了一个名为属性 RelatedItemSourcePropertyAttribute 这样的 -

I created an attribute named RelatedItemSourcePropertyAttribute like this -

/// <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;
    }
}

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

this attribute will take the property name of the related item source property(whose value will be used to fill the dropdown). It will be used like this -

    [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

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

and then in property I binded the related item source property with the combobox.

希望能看到一个更好的解决方案,那么这样的:)

Hope to see a better solution then this :)