首页 文章

Windows Phone上的Listview:未正确列出的项目

提问于
浏览
1

我的要求是为我的用户选择一个国家/地区列表以及他们的电话号码 . 在我的绝望尝试中,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级别,但我不知道在哪里 .

enter image description here

请帮我弄清楚问题所在 . 提前致谢 :)

1 回答

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

相关问题