Windows Phone 上的列表视图:项目未正确列出
Listview on Windows Phone: Items not listing correctly
我的要求是为我的用户 select 和他们的 phone 号码实施一个国家列表。
在我绝望的尝试中,this 是我关注的文章。
我以这种方式设置了 ListView:
国家名称是试用的,所以没有意义。
ViewModel:我在其中对国家/地区列表进行了硬编码
//Contructor class
public CountryList_test()
{
//Creating static data memberrs (Hardcoded logic)
var list_countryList = new List
{
new CountryList_CountryNames{countryName = "ffsdfndf"},
new CountryList_CountryNames{countryName = "hfhjf"},
new CountryList_CountryNames{countryName = "vcxbv"},
new CountryList_CountryNames{countryName = "ujhfg"},
new CountryList_CountryNames{countryName = "vbcvb"},
new CountryList_CountryNames{countryName = "fdfgd"},
new CountryList_CountryNames{countryName = "dfdhgfjjyhg"},
new CountryList_CountryNames{countryName = "khjkj"},
new CountryList_CountryNames{countryName = "fffg"},
};
//Grouping the country names
var countriesByCategories = list_countryList.GroupBy(x => x.countryName).Select(x => new CountryList_GroupMechanism { countryNameForGroupMechanism = x.Key,countryListForGroupMechanism = x.ToList() });
countryListForGroupMechanism = countriesByCategories.ToList();
}
public class CountryList_CountryNames
{
public string countryName { get; set; }
//public int internationalCode { get; set; }
}
public class CountryList_GroupMechanism
{
public string countryNameForGroupMechanism { get; set; }
public List countryListForGroupMechanism { get; set; }
}
}
This looks fine.
This is my Model: It is in the constructor of the class Bpage2
<pre>
public Bpage2()
{
//creating the instance of the View Model and setting it as the data context of the page
this.InitializeComponent();
var viewModel = new CountryList_test();
this.DataContext = viewModel;
}
This is my View:
<!--countryListForGroupMechanism comes from the ViewModel-->
<ListView Name="lstv_countries"
ItemsSource="{Binding countryListForGroupMechanism}" >
</ListView>
这是我的输出,它让我抓狂。我确定问题出在 V-M 级别,但我不知道在哪里。
请帮我看看问题出在哪里。提前致谢:)
如果您希望看到国家/地区名称,请设置 DisplayMemberPath,因此应执行以下操作:
<ListView Name="lstv_countries" DisplayMemeberPath="countryNameForGroupMechanism"
ItemsSource="{Binding countryListForGroupMechanism}" >
</ListView>
或使用 ItemTemplate
<ListView Name="lstv_countries" ItemsSource="{Binding countryListForGroupMechanism}" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=countryNameForGroupMechanism}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我的要求是为我的用户 select 和他们的 phone 号码实施一个国家列表。 在我绝望的尝试中,this 是我关注的文章。 我以这种方式设置了 ListView:
国家名称是试用的,所以没有意义。
ViewModel:我在其中对国家/地区列表进行了硬编码
//Contructor class public CountryList_test() { //Creating static data memberrs (Hardcoded logic) var list_countryList = new List { new CountryList_CountryNames{countryName = "ffsdfndf"}, new CountryList_CountryNames{countryName = "hfhjf"}, new CountryList_CountryNames{countryName = "vcxbv"}, new CountryList_CountryNames{countryName = "ujhfg"}, new CountryList_CountryNames{countryName = "vbcvb"}, new CountryList_CountryNames{countryName = "fdfgd"}, new CountryList_CountryNames{countryName = "dfdhgfjjyhg"}, new CountryList_CountryNames{countryName = "khjkj"}, new CountryList_CountryNames{countryName = "fffg"}, }; //Grouping the country names var countriesByCategories = list_countryList.GroupBy(x => x.countryName).Select(x => new CountryList_GroupMechanism { countryNameForGroupMechanism = x.Key,countryListForGroupMechanism = x.ToList() }); countryListForGroupMechanism = countriesByCategories.ToList(); } public class CountryList_CountryNames { public string countryName { get; set; } //public int internationalCode { get; set; } } public class CountryList_GroupMechanism { public string countryNameForGroupMechanism { get; set; } public List countryListForGroupMechanism { get; set; } } }
This looks fine.
This is my Model: It is in the constructor of the class Bpage2
<pre>
public Bpage2()
{
//creating the instance of the View Model and setting it as the data context of the page
this.InitializeComponent();
var viewModel = new CountryList_test();
this.DataContext = viewModel;
}
This is my View:
<!--countryListForGroupMechanism comes from the ViewModel-->
<ListView Name="lstv_countries"
ItemsSource="{Binding countryListForGroupMechanism}" >
</ListView>
这是我的输出,它让我抓狂。我确定问题出在 V-M 级别,但我不知道在哪里。
请帮我看看问题出在哪里。提前致谢:)
如果您希望看到国家/地区名称,请设置 DisplayMemberPath,因此应执行以下操作:
<ListView Name="lstv_countries" DisplayMemeberPath="countryNameForGroupMechanism"
ItemsSource="{Binding countryListForGroupMechanism}" >
</ListView>
或使用 ItemTemplate
<ListView Name="lstv_countries" ItemsSource="{Binding countryListForGroupMechanism}" >
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=countryNameForGroupMechanism}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>