2012-04-25 36 views
-3

如何列出的名字,一個類的所有屬性名稱的Silverlight C#WP - 房源來源的綁定屬性類

<ListBox> 
<TextBlock Name="{Binding WHAAAAT???!}" /> 
</ListBox> 

類:

public class DaysOfWeek 
{  
    public bool Monday {get; set;} 
    public bool Tuesday { get; set; } 
    public bool Wednesday { get; set; } 
    public bool Thursday { get; set; } 
    public bool Friday { get; set; } 
    public bool Saturday { get; set; } 
    public bool Sunday { get; set; } 
} 

我希望把這個內容在列表框中。 請幫助我。

Monday 
Tuesday 
Wednesday 
Thursday 
Friday 
Saturday 
Sunday 

不勝感激。

+2

您DaysOfWeek應該是一個枚舉,然後你應該有類型DaysOfWeek的CURRENTDAY財產另一類 – earthling 2012-04-25 23:47:05

回答

2

聽起來像是你將需要使用反射,如圖here

using System.Reflection; // reflection namespace 

// get all public static properties of MyClass type 
PropertyInfo[] propertyInfos; 
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public | 
               BindingFlags.Static); 
// sort properties by name 
Array.Sort(propertyInfos, 
     delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2) 
     { return propertyInfo1.Name.CompareTo(propertyInfo2.Name); }); 

// write property names 
foreach (PropertyInfo propertyInfo in propertyInfos) 
{ 
    Console.WriteLine(propertyInfo.Name); 
} 
+1

真棒!謝謝!!!!!我嘗試過類似的東西! – Richard 2012-04-25 22:07:30

+1

可能使用獲取類型並返回公共屬性列表的轉換器。 – 2012-04-25 22:18:30

1

我的解決辦法:

Classes.DaysOfWeek _DaysOfWeek; 

_DaysOfWeek = new Classes.DaysOfWeek(); 
var listProp = _DaysOfWeek.GetType().GetProperties().ToList(); 

List<String> newList = new List<String>{}; 
foreach(var item in listProp){ 
newList.Add(item.Name); 
} 
listBox_Days.ItemsSource = newList; 

易於理解!

0

查找到MVVM模型(通過例如,創建一個新的全景/樞軸/數據綁定示例應用程序) 這是在試圖寫新代碼, 和保持非常乾淨的同時,將減少的時間最大量的模型。

祝你好運;)