如何从所选项目ListPicker获得内容文本Windows Phone 8.1 Silverlight



我在这里有一个问题,从我的ListPicker中的选定项目中获取文本,我发现这个代码允许我这样做

var content = ((ListPickerItem)CursoLista.SelectedItem).Content;

和我的XAML:

<toolkit:ListPicker x:Name="CursoLista" Header="Curso" ItemsSource="{Binding}">
                    <toolkit:ListPicker.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <toolkit:ListPickerItem Content="{Binding Curso}"/>
                            </StackPanel>
                        </DataTemplate>
                    </toolkit:ListPicker.ItemTemplate>
                </toolkit:ListPicker>

你可以看到,内容是绑定到一个列表从我的服务器:

private void Cliente_ProfessorRetrieveCompleted(object sender, Service.ProfessorRetrieveCompletedEventArgs e)
    {
        CursoLista.ItemsSource = e.Result;

但是当我尝试这样做时,我有一个异常:

Additional information: Unable to cast object of type 'FaculdadeGuararapes.Service.ListaProfessor' to type 'Microsoft.Phone.Controls.ListPickerItem'.

FaculdadeGuararapes.Service。ListaProfessor是来自我的WebServer的列表!

首先,如果您的e.Result是一个字符串列表,并且您从后面的代码设置了CursoLista.ItemsSource,则不需要在xaml中添加ItemsSource。接下来,如果您想检索单个选中的项目,只需锁定SelectionChanged事件。此外,可能没有必要添加<DataTemplate>DataTemplate仅在您想要自定义布局或绑定到类时才需要。

<toolkit:ListPicker x:Name="CursoLista" Header="Curso" SelectionChanged="CursoLista_SelectionChanged">
</toolkit:ListPicker>

并在后面的代码中处理:

    private void CursoLista_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
         string selectedString = (sender as ListPicker).SelectedItem as string;
    }

最新更新