3

I am trying to set the value/selecteditem of a listpicker control - from the silverlight toolkit for windows phone 7 (when the user wants to edit an entry in xml, it pulls the data out of IO and sets it in the text boxes/listpickers).

I am currently trying to use:

ListPickerSub.SelectedItem = sub;

(sub is a string)

But it is throwing a System.InvalidOperationException

Additional information: SelectedItem must always be set to a valid value.
2
  • 1
    What is sub? Does it actually belong to the list? Dec 12, 2010 at 0:30
  • it is a value of one of the elements in storage yes, say i was using xElement("subElement").Value to set the data binding. sub is a identical value to the value in subElement. I have also tried (just to test) by setting the listpicker's content statically: <sys:String>monday</sys:String> <sys:String>sunday</sys:String>
    – Jamie
    Dec 12, 2010 at 10:29

3 Answers 3

3

SelectedItem is expecting a ListPickerItem (which is one of the items in the list). You're passing it a string - hence the error.

You may find it easier to set the SelectedIndex.

It's hard to give a relevant example of setting the SelectedItem without knowing what you're populating the list with.

Edit:
Here's an example of how you could bind to strings. Without a workable example of what you are actually binding to the itemsource this is hte best I can do. (Just giving the name of the object or partial code isn't enough.)

Assuming:

<Controls:ListPicker x:Name="ListPickerSub">
    <Controls:ListPicker.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </Controls:ListPicker.ItemTemplate>
    <Controls:ListPicker.FullModeItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </Controls:ListPicker.FullModeItemTemplate>
</Controls:ListPicker>

Then I can bind the contents with:

ListPickerSub.ItemsSource = SubItems();


private IEnumerable<string> SubItems()
{
    yield return "monday";
    yield return "tuesday";
    yield return "wednesday";
    yield return "thursday";
    yield return "friday";
    yield return "saturday";
    yield return "sunday";
}

and set the SelectedItem with:

ListPickerSub.SelectedItem = "sunday";
5
  • I am populating the list like this: <toolkit:ListPicker.ItemTemplate> <DataTemplate> <TextBlock Name="{Binding subName}" Text="{Binding subName}" Margin="0"/> </DataTemplate> </toolkit:ListPicker.ItemTemplate>
    – Jamie
    Dec 13, 2010 at 10:01
  • @Jamie but what are you passing as the ItemSource?
    – Matt Lacey
    Dec 13, 2010 at 11:27
  • Binding subName. I did try using: object index = SubTxt.FindName(sub); then setting the selecteditem as index but index always returned as null
    – Jamie
    Dec 13, 2010 at 11:51
  • Thank you so much, that is working perfectly. Only problem i am having is in my settings where i set the listpicker like so: <sys:string>monday</sys:string> for each value and as with all of my settings, i have used a two way binding to set the selecteditem: SelectedItem="{Binding Source={StaticResource AppSettings}, Path=WeekBeginsSetting, Mode=TwoWay} This binding returns the correct value because I have used it to set the text of a textblock (just to test). But when trying to set the selecteditem it throws a System.ArgumentOutOfRangeException
    – Jamie
    Dec 13, 2010 at 20:24
  • @Jamie please ask this new question as a new question. If you can post a complete example which demonstrates the problem this will help you get a faster, more relevant answer. See also stackoverflow.com/faq#howtoask
    – Matt Lacey
    Dec 14, 2010 at 9:19
2

Something like the following:

ListPickerSub.SelectedItem = ListPickerSub.Items.First(x => (x as ListPickerItem).Content.ToString() == sub);

You may need to cast the Content to TextBlock, and change the code accordingly. The above works for my case where ListPicker is populated dynamically using ListPickerItem.

0

Surely you want to set the data in the underlying datasource and then just refresh your list?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.