請有人告訴我如何只列出我想在PropertyGrid
中顯示的屬性。在PropertyGrid中顯示的過濾屬性
示例創建列表或屬性並僅顯示該列表中的屬性。
這裏有一個很好的屬性網格示例,就是我現在使用的。
http://hotfile.com/dl/104485386/ce9e469/PropertyGridDemo.rar.html
如果可以粘貼示例代碼我感謝這麼多。
請有人告訴我如何只列出我想在PropertyGrid
中顯示的屬性。在PropertyGrid中顯示的過濾屬性
示例創建列表或屬性並僅顯示該列表中的屬性。
這裏有一個很好的屬性網格示例,就是我現在使用的。
http://hotfile.com/dl/104485386/ce9e469/PropertyGridDemo.rar.html
如果可以粘貼示例代碼我感謝這麼多。
如果您查看代碼,則僅添加可瀏覽的屬性。
if (!property.IsBrowsable) continue;
所以,如果你不想顯示一個屬性使它成爲不可瀏覽。你可以這樣做
[Browsable(false)]
如果您不希望顯示在屬性網格的屬性只需要提供可瀏覽屬性,這樣設置爲假做。
[Browsable(false)]
public SolidColorBrush Background { get; set; }
希望它可以幫助
可以使用可瀏覽屬性作爲Anuraj說,或者如果你想要更多的控制 (如果您是使用其他類/控件派生定製控件),您可以創建自己的屬性並使用它來過濾屬性。
這裏是你如何能做到這一點 -
步驟1 - 創建自定義屬性
/// <summary>
/// Attribute to identify the Custom Proeprties.
/// Only Proeprties marked with this attribute(true) will be displayed in property grid.
/// </summary>
[global::System.AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class IsCustomPropertyAttribute : Attribute
{
// See the attribute guidelines at
// http://go.microsoft.com/fwlink/?LinkId=85236
private bool isCustomProperty;
public IsCustomPropertyAttribute(bool isCustomProperty)
{
this.isCustomProperty = isCustomProperty;
}
public bool IsCustomProperty
{
get { return isCustomProperty; }
set { isCustomProperty = value; }
}
public override bool IsDefaultAttribute()
{
return isCustomProperty == false;
}
}
步驟 - 2
在你的控制(其屬性要顯示)用這個屬性標記每個屬性 -
[IsCustomProperty(true)]
[DisplayName("Orientation")]
public bool ScaleVisibility
{
get { return (bool)GetValue(ScaleVisibilityProperty); }
set { SetValue(ScaleVisibilityProperty, value); }
}
public static readonly DependencyProperty ScaleVisibilityProperty =
DependencyProperty.Register("ScaleVisibility", typeof(bool),
typeof(IC_BarControl), new UIPropertyMetadata(true));
步驟-3現在
,在您的屬性網格代碼(如果您要添加的屬性網格屬性)增加一個檢查該屬性這樣的 -
//Check if IsCustomPropertyAttribute is defined for this property or not
bool isCustomAttributeDefined = Attribute.IsDefined(type.GetProperty
(propertyDescriptor.Name), typeof(IsCustomPropertyAttribute));
if (isCustomAttributeDefined == true)
{
//IsCustomPropertyAttribute is defined so get the attribute
IsCustomPropertyAttribute myAttribute =
Attribute.GetCustomAttribute(type.GetProperty(propertyDescriptor.Name),
typeof(IsCustomPropertyAttribute)) as IsCustomPropertyAttribute;
//Check if current property is Custom Property or not
if (myAttribute != null && myAttribute.IsCustomProperty == true)
{
AddProperty(propertyDescriptor);
}
}