【问题标题】:Listview on Windows Phone: Items not listing correctlyWindows Phone 上的 Listview:项目未正确列出
【发布时间】:2015-06-02 03:20:37
【问题描述】:

我的要求是实现一个国家列表供我的用户选择和他们的电话号码。 在我绝望的尝试中,this 是我关注的文章。 我有一个这样的 ListView 设置:

国家名称是试用的,所以没有意义。

ViewModel:我在哪里硬编码国家/地区列表

//构造函数类 公共 CountryList_test() { //创建静态数据成员(硬编码逻辑) var list_countryList = 新列表 { 新 CountryList_CountryNames{countryName = "ffsdfndf"}, 新 CountryList_CountryNames{countryName = "hfhjf"}, 新 CountryList_CountryNames{countryName = "vcxbv"}, 新 CountryList_CountryNames{countryName = "ujhfg"}, 新 CountryList_CountryNames{countryName = "vbcvb"}, 新 CountryList_CountryNames{countryName = "fdfgd"}, 新 CountryList_CountryNames{countryName = "dfdhgfjjyhg"}, 新 CountryList_CountryNames{countryName = "khjkj"}, 新 CountryList_CountryNames{countryName = "fffg"}, }; //对国家名称进行分组 var countriesByCategories = list_countryList.GroupBy(x => x.countryName).Select(x => new CountryList_GroupMechanism { countryNameForGroupMechanism = x.Key,countryListForGroupMechanism = x.ToList() }); countryListForGroupMechanism = countriesByCategories.ToList(); } 公共类 CountryList_CountryNames { 公共字符串 countryName { 获取;放; } //public int internationalCode { get;放; } } 公共类 CountryList_GroupMechanism { 公共字符串 countryNameForGroupMechanism { 获取;放; } 公共列表 countryListForGroupMechanism { get;放; } } }
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 级别,但我不知道在哪里。

请帮我找出问题所在。在此先感谢:)

【问题讨论】:

  • 抱歉格式化。不知道这是怎么发生的。
  • 对不起,我错过了:这里是:public List countryListForGroupMechanism { get;放; }。所以它是一个列表。

标签: c# xaml data-binding windows-phone-8.1 viewmodel


【解决方案1】:

如果您希望看到国家/地区名称,请设置 DisplayMemeberPath,因此应执行以下操作:

<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>

【讨论】:

  • DisplayMemeberPath 和 ItemTemplate 都不起作用。它只显示一个空白屏幕。里面没有内容。可能出了什么问题?
  • @AnonymousPerson 现在?!我不太了解你的模型
猜你喜欢
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多