WPF PropertyGrid - 添加对集合的支持

发布于 2024-09-01 09:28:06 字数 322 浏览 2 评论 0原文

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

有什么想法吗?

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

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.

Any ideas?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2024-09-08 09:28:06

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

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

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

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

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

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文