2010-05-12 137 views
2

我正在處理wpf PropertyGrid(PG)控件,我希望PG支持集合類型(IListObservableCollection等)屬性。我對如何跟蹤(該集合的)選定項目並將其傳遞給客戶端有點困惑。WPF PropertyGrid - 添加對集合的支持

任何想法?

如果解決方案使用開源WPF PropertyGrid(http://www.codeplex.com/wpg),我會將更改/添加回執行到控件中。

回答

2

沒有答案證明沒有直接的做法。所以,我實現了這個功能,這樣一來 -

我創建了一個名爲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 

然後在屬性中綁定相關的項目源屬性與組合框。

希望看到更好的解決方案,然後:)

+0

我有同樣的情況。我需要爲WPG添加收集支持。我實現了這個代碼;但我認爲還不夠。 – 2010-10-22 08:30:22

+0

@Bahadir arslan:你能否提供更多關於你所面臨的具體問題/錯誤的細節?我在我的應用程序中實現了相同的功能,並且工作正常。 – akjoshi 2010-10-22 11:14:14

+0

我需要顯示一個屬性作爲列表。例如,用戶可以在城市列表中選擇城市。所以我找到了你的帖子;但我無法達到:)當我實現你的解決方案時,首先依賴項屬性在屬性網格上變得可見,其次我的屬性(SelectedUnitProperty)顯示爲TextBox。之後,我找到了WPGTemplates.xaml並編輯了IList類型模板;所以我可以顯示我的列表爲組合框:) – 2010-10-22 12:00:26