1
votes

XAML

 <DataTemplate x:Name="PickTmplItemTipo">
            <StackPanel Orientation="Horizontal">               
                <TextBlock Text="{Binding tipo}" />                
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Name="PickTmplFullTipo">            
            <StackPanel Orientation="Horizontal" Margin="0,25,0,0">
                <TextBlock Name="lblTipo" Width="350" Text="{Binding tipo}" FontFamily="{StaticResource PhoneFontFamilyLight}" TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeExtraLarge}" />
            </StackPanel>
        </DataTemplate>


<toolkit:ListPicker    
         Grid.Row="0"
         ItemsSource="{Binding}" Margin="21,0,22,0" 
         Header="{Binding Source={StaticResource LocalizedStrings}, Path=Localizedresources.strTipoUni}" 
         FullModeHeader="{Binding Source={StaticResource LocalizedStrings}, Path=Localizedresources.strTipoUni}" 
         FullModeItemTemplate="{Binding Source={StaticResource PickTmplFullTipo}}" 
         ItemTemplate="{Binding Source={StaticResource PickTmplItemTipo}}" 
         Name="lPickTipo" 
         TabIndex="0" 
         Height="98" 
         VerticalAlignment="Top" 
         ExpansionMode="FullScreenOnly" 
         Tap="lPickTipo_Tap" 
         SelectionChanged="lPickTipo_SelectionChanged" />

C#

Fill listpicker:

    List<tipos> _lstTipos { get; set; }
    private void cargaLista()
    {
        using (serviciosDBDataContext miDataContext = new serviciosDBDataContext(conn))
        {
            _lstTipos = miDataContext.tipos.ToList();
        }

        this.lPickTipo.ItemsSource = _lstTipos;
    }

Set selecteditem:

if I try this, returns this error "SelectedItem must always be set to a valid value."

this.lPickTipo.SelectedItem = myStringValue;

And if I try the next thing, returns null error:

this.lPickTipo.SelectedItem = lPickTipo.Items.First(x => (x as ListPickerItem).Content.ToString() == myStringValue);

But I can not to set by selectindex because I dont know which index equival

3

3 Answers

5
votes
  • If you have a reference to the tipo (or if you override the tipo's equality operator):

    this.lPickTipo.SelectedItem = yourTipo;
    

(for instance: this.lPickTipo.SelectedItem = _lstTipos[2];)

  • Otherwise:

    this.lPickTipo.SelectedItem = this.lPickTipo.Items.OfType<tipos>().First(i => i.tipo == myStringValue); 
    
1
votes

Your list is bound to items of type tipos but you're trying to set the selected item to a string value, which it will not find. The SelectedItem property expects one of the bound items, or `null if no one is selected.

Try setting it to one of the values in the _lstTipos list, e.g:

this.lPickTipo.SelectedItem = _listTipos.First();

PS. I have not tried this ListPicker control, but I believe this is how it usually works with WPF controls.

BTW looks like this is a dupe of this.

1
votes

What if you try using a Linq request to retrieve the index?

assuming you don't have any duplicate in your list

this.lPickTipo.SelectedItem = _lstTipos.IndexOf(_lstTipos.Single(s => s == myStringValue));