How to get id values from listpicker?

纵然是瞬间 提交于 2019-12-11 19:14:18

问题


I'm developing windows Phone 8 application.

In my app i use List Picker .list picker value is bind from web service (json format)

My web service result return format

[
   - {
       id: "9",
       name: "Pizza",
       root_id: "4",
       level: "1"
     },
    -{
       id: "10",
       name: "Fine Dining",
       root_id: "4",
       level: "1"
     },
]

My XAML code for list picker

  <toolkit:ListPicker x:Name="filterpicker"  SelectionChanged="filterpicker_SelectionChanged" Tap="filterpicker_Tap" Foreground="White" BorderThickness="0" Margin="395,-100,0,10"  Height="70" Width="60">

            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <TextBlock  Text="{Binding}" Visibility="Collapsed"/>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>

            <toolkit:ListPicker.FullModeItemTemplate>
                <DataTemplate>
                    <TextBlock>
                        <Run Text="{Binding}"/>
                    </TextBlock>
                </DataTemplate>
            </toolkit:ListPicker.FullModeItemTemplate>
        </toolkit:ListPicker>

My c# code for bind

 void filterwc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {

        var filterdata = e.Result;
        var filtervalue = JsonConvert.DeserializeObject<List<filterdatas>>(filterdata);
        List<string> filtercategories = new List<string>();
        foreach (filterdatas budval2 in filtervalue)
        {
          filtercategories.Add(budval2.id); /**I need to display the name and on selectionchanged event i need to get the id value**/
        }

        filterpicker.ItemsSource = filtercategories;
    }

if my question is not clear plz tell i give more info ...

I need Like this

List picker out put come like this

Pizza

Fine Dining

On SelectionChanged Event

if i click Pizza I need to get "9"

if i click Fine Dining I need to get "10"

Thank you...


回答1:


        <toolkit:ListPicker.ItemTemplate>
            <DataTemplate>
                <TextBlock  Text="{Binding name}" Visibility="Collapsed"/>
            </DataTemplate>
        </toolkit:ListPicker.ItemTemplate>

        <toolkit:ListPicker.FullModeItemTemplate>
            <DataTemplate>
                <TextBlock>
                    <Run Text="{Binding name}"/>
                </TextBlock>
            </DataTemplate>
        </toolkit:ListPicker.FullModeItemTemplate>

code Behind:

    var filterdata = e.Result;
    var filtervalue = JsonConvert.DeserializeObject<List<filterdatas>>(filterdata);
    filterpicker.ItemsSource = filtervalue;

selection changed event

if(filterpicker.SelectedItem == null) 
   return;
filterdatas element = filterpicker.SelectedItem as filterdatas;
MessageBox.Show("This is the id:"+element.id);


来源:https://stackoverflow.com/questions/22966182/how-to-get-id-values-from-listpicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!