簡體   English   中英

如何從選定的項目ListPicker Windows Phone 8.1 Silverlight獲取內容文本?

[英]How to get the content text from the selected item 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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM