我試圖在屬性網格中顯示我的Wall類的屬性。現在我的牆上類看起來是這樣的:在Windows窗體的屬性網格中正確顯示自定義對象的列表
[TypeConverter(typeof(ExpandableObjectConverter))]
public class WallType
{
#region _private variables and Properties
public string Name { get; set; } //Name of the wall to identify it. For the wall program, it is automatically generated (ex. E4-1 where E = exterior wall, 4 = 2x4, and 1 means first exterior wall defined)
public string Type { get; set; } //Type of wall (exterior, interior, etc.)
public bool IsMirrorable { get; set; } //Identifies if the wall is mirrorable or not
public Distance Height { get; set; } //Height of the wall from bottom to talk
public string StudPattern { get; set; } //Pattern in which the studs of the wall are laid out
public Distance StudSpacing { get; set; } //Spacing between each stud
public VaporBarrier Barrier { get; set; }
public LetIn LetInOfWall { get; set; }
public Sheathing SheathingUsed { get; set; }
[TypeConverter(typeof(ExpandableObjectConverter))]
public List<Member> Members { get; set; }
public bool HasLetIn { get; set; }
public bool HasCapPlate { get; set; }
public bool HasVaporBarrier { get; set; }
public bool HasSheathing { get; set; }
#endregion
/*Constructors and Methods not shown*/
}
當我在屬性網格顯示WallType的一個實例,它出來是這樣的:
一切看起來都不錯,除了成員屬性。現在它所做的只是顯示容量和數量。我想要的是它顯示列表中每個成員的名稱屬性。我認爲把ExpandableObjectConverter放在列表上可以解決問題,但它沒有。所以,我試圖把一個自定義類型轉換器上的成員類的頂部
[TypeConverter(typeof(MemberObjectConverter))]
public class Member
{
#region _private variables and Properties
public string Name { get; set; } //Name of the member used to uniquely identify it
public string Size { get; set; } //Size of the member (ex. 2x4)
public string Grade { get; set; } //Grade of the wood used to make the member
public Distance Length { get; set; } //Length of the member
public string Species { get; set; } //Type of wood the member is made of
public string Treatment { get; set; } //Type of treatment applied to the member
public string Other { get; set; } //Variable for other notes about the member
#endregion
/*Constructors and Methods not shown*/
}
最後,我的自定義類型轉換
namespace Wall_Program
{
/// <summary>
/// Extending the ExpandableObjectConverter to display member objects properly
/// </summary>
public class MemberObjectConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(Member))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, System.Type destinationType)
{
if (destinationType == typeof(System.String) && value is Member)
{
Member m = (Member)value;
return "Name: " + m.Name +
", Size: " + m.Size +
", Grade: " + m.Grade +
", Length: " + m.Length.Architectural +
", Species: " + m.Species +
", Treatment: " + m.Treatment +
", Other: " + m.Other;
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
try
{
string s = (string)value;
int colon = s.IndexOf(':');
int comma = s.IndexOf(',');
if (colon != -1 && comma != -1)
{
string name = s.Substring(colon + 1, (comma - colon - 1));
colon = s.IndexOf(':', comma + 1);
comma = s.IndexOf(',', comma + 1);
string size = s.Substring(colon + 1, (comma - colon - 1));
colon = s.IndexOf(':', comma + 1);
comma = s.IndexOf(',', comma + 1);
string grade = s.Substring(colon + 1);
colon = s.IndexOf(':', comma + 1);
comma = s.IndexOf(',', comma + 1);
Distance length = new Distance(s.Substring(colon + 1, (comma - colon - 1)));
colon = s.IndexOf(':', comma + 1);
comma = s.IndexOf(',', comma + 1);
string species = s.Substring(colon + 1, (comma - colon - 1));
colon = s.IndexOf(':', comma + 1);
comma = s.IndexOf(',', comma + 1);
string treatment = s.Substring(colon + 1, (comma - colon - 1));
colon = s.IndexOf(':', comma + 1);
string other = s.Substring(colon + 1);
Member m = new Member(name, size, grade, length, species, treatment, other);
return m;
}
}
catch
{
throw new ArgumentException("Can not convert '" + (string)value + "' to type Member");
}
}
return base.ConvertFrom(context, culture, value);
}
}
}
的自定義類型轉換是有一定的效果,因爲當我手動單擊按鈕顯示收集所有內容都已正確列出。但我想要的是名稱是顯示的內容而不是容量和數量。
這可能是相關的:HTTP://stackoverflow.com/questions/6307006/how -can-i-use-a-winforms-propertygrid-to-edit-a-list-of-strings – NoChance
它是一個集合。這種類型的轉換器只是將這兩部分摺疊起來。你所要求的是一種顯示收集物品的非常奇怪的方式。你也會編輯它們嗎?如果你想'Members'集合屬性做些什麼,你需要一個自定義的轉換器。看起來成員項目轉換器有一些問題,但目前爲止它不會運行 – Plutonix