简体   繁体   English

如何从选定的项目ListPicker Windows Phone 8.1 Silverlight获取内容文本?

[英]How to get the content text from the selected item ListPicker Windows Phone 8.1 Silverlight?

I have a problem here to take the text from selected item inside of my ListPicker,i found this code who allows me to do that 我在这里无法从ListPicker的选定项目中提取文本,我发现此代码允许我执行此操作

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

and my XAML: 和我的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>

as you can see,the content is Binding to a List from my server: 如您所见,内容是从我的服务器绑定到列表:

private void Cliente_ProfessorRetrieveCompleted(object sender, Service.ProfessorRetrieveCompletedEventArgs e)
    {

        CursoLista.ItemsSource = e.Result;

but when i try to do this,i have an Execption : 但是当我尝试这样做时,我有一个例外:

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

FaculdadeGuararapes.Service.ListaProfessor its the list who comes from my WebServer! FaculdadeGuararapes.Service.ListaProfessor或它是来自我的WebServer的列表!

First of all, if your e.Result is a list of strings and you set CursoLista.ItemsSource from code behind, you don't need to add ItemsSource in xaml. 首先,如果您的e.Result是一个字符串列表,并且从后面的代码中设置了CursoLista.ItemsSource ,则无需在xaml中添加ItemsSource Next, if you want to retreive single selected item, just pin to SelectionChanged event. 接下来,如果要检索单个所选项目,只需将其固定到SelectionChanged事件。 Additionally, probably it's not necessary to add <DataTemplate> . 另外,可能没有必要添加<DataTemplate> DataTemplate is only necessary when you want to customize a layout or bind to the class. 仅当您要自定义布局或绑定到类时才需要DataTemplate

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

and handle it in code behind: 并在后面的代码中处理它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM