1

when I bind data to the listpicker, I get name displayed in selected item, but when the dropdown shows the whole item will be myProject.MyClass....something I am new to windows phone application development.. Can anyone help me with this?

here is my xaml code

<toolkit:ListPicker Header="Select Name" Name="listName" Tap="listName_Tap" VerticalAlignment="Center">
   <toolkit:ListPicker.ItemTemplate>
        <DataTemplate>
            <StackPanel>
            <TextBlock Text="{Binding name}"/>
            </StackPanel>
        </DataTemplate>
      </toolkit:ListPicker.ItemTemplate> 
</toolkit:ListPicker>

Here is my c# code

class MyClass
{
    public String name{ get; set; }
}

private void sendPostCompleted(object sender, UploadStringCompletedEventArgs e)     
{
    //e.result is my json string obtained from webservice([{name:hhh}{name:jkj}{name:jack}])
    var data = JsonConvert.DeserializeObject<MyClass[]>(e.Result.ToString()); 
    listName.ItemsSource = data;
}
MoLow
  • 2,990
  • 2
  • 17
  • 39
shahna
  • 9
  • 5

2 Answers2

0

I am not so sure but I think the problem is with the binding. You're binding an Array, instead of it try List:

var data = JsonConvert.DeserializeObject<List<MyClass>>(e.Result.ToString()); 

You also need to add JsonProperty to your class property (ref):

class MyClass
{
    [JsonProperty("name")]
    public String name{ get; set; }
}
Community
  • 1
  • 1
Shaharyar
  • 11,393
  • 3
  • 39
  • 59
  • hi sorry fore the delay in reply.. actually the problem was with setting a full mode item template. i didnt set fullmodeitem template before.. anyway thanks for your reply.. – shahna Dec 30 '15 at 06:22
  • @shahna It would be great if you answer your own question and accept it. It will help others in future. – Shaharyar Dec 30 '15 at 06:27
0

Try this below code :

<toolkit:ListPicker Header="Select Name" Name="listName" Tap="listName_Tap" VerticalAlignment="Center" ItemsSource={Binding NamesList}>
   <toolkit:ListPicker.ItemTemplate>
        <DataTemplate>
            <StackPanel>
            <TextBlock Text="{Binding name}"/>
            </StackPanel>
        </DataTemplate>
      </toolkit:ListPicker.ItemTemplate> 
</toolkit:ListPicker>

In Code behind, Add the following :

private ObservableCollection<MyClass> _NamesList = new ObservableCollection<MyClass>();    
public ObservableCollection<MyClass> NamesList 
{ 
   get {return _NamesList} 
   set {_NamesList = value}
}

Add below code in constructor

this.DataContext = this;

Copy the array data to observablecollection

private void sendPostCompleted(object sender, UploadStringCompletedEventArgs e)     
    {
        //e.result is my json string obtained from webservice([{name:hhh}{name:jkj}{name:jack}])
        var data = JsonConvert.DeserializeObject<MyClass[]>(e.Result.ToString()); 

            foreach (var item in data)
            {
                NamesList.Items.Add(item);
            }
    }

Note : Use ObservableCollection for binding list UI controls for updating UI data while property changes.

sriman reddy
  • 753
  • 8
  • 22
  • hi, it shows an error like, Error 1 Inconsistent accessibility: property type 'System.Collections.ObjectModel.ObservableCollection' is less accessible than property 'MyProject.Views.ViewNames.NamesList' – shahna Dec 23 '15 at 07:17
  • Make your MyClass Public ... public class MyClass { [JsonProperty("name")] public String name{ get; set; } } – sriman reddy Dec 23 '15 at 08:39
  • hi sorry fore the delay in reply.. actually the problem was with setting a full mode item template. i didnt set fullmodeitem template before.. anyway thanks for your reply.. – shahna Dec 30 '15 at 06:22