如何从listpicker获取id值?

时间:2014-04-09 14:46:54

标签: c# xaml windows-phone-8 listpicker

我正在开发Windows Phone 8应用程序。

在我的应用程序中,我使用List Picker .list选择器值从Web服务绑定(json格式)

我的网络服务结果返回格式

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

我的列表选择器的XAML代码

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

我的c#代码用于绑定

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

如果我的问题不明确,请告诉我提供更多信息......

我需要像这样

列表选择器出来就像这样

比萨饼

美食

在SelectionChanged事件

如果我点击披萨我需要得到“9”

如果我点击Fine Dining,我需要得到“10”

谢谢...

1 个答案:

答案 0 :(得分: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>

代码背后:

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

选择更改事件

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