Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight C#WP-列出类的属性名称_C#_Silverlight_Windows Phone 7_Xaml - Fatal编程技术网

Silverlight C#WP-列出类的属性名称

Silverlight C#WP-列出类的属性名称,c#,silverlight,windows-phone-7,xaml,C#,Silverlight,Windows Phone 7,Xaml,如何通过绑定列出类的所有属性名称 <ListBox> <TextBlock Name="{Binding WHAAAAT???!}" /> </ListBox> 我想将此内容放在列表框中。 请帮我做这个 Monday Tuesday Wednesday Thursday Friday Saturday Sunday 非常感谢。听起来您需要如图所示使用反射 我的解决方案: Classes.DaysOfWeek _DaysOfWeek; _DaysO

如何通过绑定列出类的所有属性名称

<ListBox>
<TextBlock Name="{Binding WHAAAAT???!}" />
</ListBox>
我想将此内容放在列表框中。 请帮我做这个

Monday
Tuesday
Wednesday 
Thursday 
Friday 
Saturday
Sunday 

非常感谢。

听起来您需要如图所示使用反射

我的解决方案:

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;
Classes.DaysOfWeek\u DaysOfWeek;
_DaysOfWeek=新类。DaysOfWeek();
var listProp=_DaysOfWeek.GetType().GetProperties().ToList();
List newList=新列表{};
foreach(listProp中的var项){
newList.Add(item.Name);
}
listBox_Days.ItemsSource=newList;
容易理解

查看MVVM模型(通过示例创建新的Panorama/Pivot/Databound示例应用程序) 这是一种在尝试编写新代码时可以减少最多时间的模型, 同时保持清洁


祝你好运;)

可能使用一个转换器来获取类型并返回该类型的公共属性列表。DaysOfWeek应为枚举,然后在另一个类中应具有DaysOfWeek类型的CurrentDay属性
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);
}
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;