如何设置则selectedItem [英] How to set the selecteditem

查看:515
本文介绍了如何设置则selectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置一个listpicker控件的值/ selectedItem设置 - 从Silverlight工具包为Windows Phone 7,当用户希望编辑XML条目(,它拉出来的数据的IO,并将其设置在文。盒/ listpickers)



我目前正在使用:

  ListPickerSub.SelectedItem =子; 



(子是一个字符串)



但它抛出一个System.InvalidOperationException

 附加信息:必须的SelectedItem始终被设置为一个有效的值。 


解决方案

的SelectedItem 期待一个ListPickerItem(这是在列表中的项目中的一个)。你传递给它一个字符串 - 因此误差



您可能会发现更容易将的SelectedIndex



这是很难给设置的SelectedItem不知道什么你填充与列表的一个例子。



编辑:结果
这里是你怎么可以绑定到字符串的例子。如果没有你实际上是绑定到的ItemSource一个可行的例子,这是最好的兴田我能做到的。 (只给该对象或部分代码的名称是远远不够的。)



假设:

 <控制:ListPicker X:NAME =ListPickerSub> 
<控制:ListPicker.ItemTemplate>
<&DataTemplate的GT;
< TextBlock的文本={结合}/>
< / DataTemplate中>
< /控制:ListPicker.ItemTemplate>
<控制:ListPicker.FullModeItemTemplate>
<&DataTemplate的GT;
< TextBlock的文本={结合}/>
< / DataTemplate中>
< /控制:ListPicker.FullModeItemTemplate>
< /控制:ListPicker>



然后,我可以与内容绑定:

  ListPickerSub.ItemsSource =子项目(); 


私人的IEnumerable<串GT;子项目()
{
收益率的回报星期一;
收益率的回报星期二;
收益率的回报周三
收益率的回报星期四;
收益率的回报星期五;
收益率的回报星期六;
收益率的回报星期日;
}

和设置的SelectedItem

  ListPickerSub.SelectedItem =星期天; 


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.

解决方案

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";

这篇关于如何设置则selectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆