问题描述
限时送ChatGPT账号..我对 WP7 中的 ListPicker
元素有一个奇怪的问题.
I have a strange problem with the ListPicker
element in WP7.
问题
通过 Binding
设置 ListPicker
的 SelectedIndex
不会改变加载后显示的默认项目.
Setting a ListPicker
's SelectedIndex
via a Binding
doesn't change the default item shown after it loads.
示例
作为测试,我快速修改了 Microsoft 的 SettingsSample
以包含一个 ListPicker
.您可以在以下位置下载:http://www.mediafire/?w0n0ymkh4dwe9b3
As a test, I've quickly modified Microsoft's SettingsSample
to include a ListPicker
. You can download it at: http://www.mediafire/?w0n0ymkh4dwe9b3
这是我们折叠起来的ListPicker
:
-----------------
| Times New Roman |
-----------------
这是展开时的 ListPicker
:
-----------------
| Times New Roman |
| Arial |
| Comic Sans MS |
-----------------
如果我们选择Arial",导航离开,然后再次返回,ListPicker
仍然显示:
If we select 'Arial', navigate away, then come back again, the ListPicker
still shows:
-----------------
| Times New Roman |
-----------------
而它应该显示(因为设置已正确保存):
Whereas it ought to show (because the settings are saved correctly):
-----------------
| Arial |
-----------------
进一步的混乱
在上面的例子中,如果我们点击ListPicker
,实际上选择了'Arial'(因为这个ListBoxItem
的文本被高亮显示).出于某种原因,它只是在加载控件时未显示为默认值.
In the above example, if we click on the ListPicker
, 'Arial' is actually selected (because the text for this ListBoxItem
is highlighted). For some reason, it's just not shown as the default value when the control is loaded.
如果我们忘记了 Binding
方法,而是在 XAML 中手动将 SelectedIndex
设置为1",那么在加载控件时我们会得到预期的结果:
If we forget about the Binding
approach, and instead manually set SelectedIndex
to '1' in XAML, then we get the expected result when the control is loaded:
-----------------
| Arial |
-----------------
想法?
在使用 Binding
时,有没有办法更新 ListPicker
控件以正确显示 SelectedIndex
?我想我遗漏了一些明显的东西,但我已经搜索过但没有找到任何东西.
Is there a way to update the ListPicker
control to show the SelectedIndex
properly when using a Binding
? I guess I'm missing something obvious, but I've searched and not been able to find anything.
提前致谢!
推荐答案
我相信 ListPicker 控件存在一个与选择绑定有关的突出错误.我认为推荐的解决方案是在页面的 OnNavigatedTo
覆盖中手动设置绑定.以下代码显示了我如何为我的一个应用程序执行此操作:
I believe there is an outstanding bug with the ListPicker control to do with binding the selection. I think the recommended solution is to manually set a binding in the OnNavigatedTo
override for the page. The following code shows how I do it for one of my apps:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Binding pickerBinding = new Binding("DistanceUnit")
{
Source = App.Current.Resources["Settings"],
Mode = BindingMode.TwoWay
};
this._distanceUnit.SetBinding(ListPicker.SelectedItemProperty, pickerBinding);
}
或者,您可以处理 ListPicker 本身的 Loaded 事件并在那里设置绑定.如果您有多个选择器,则前一种方法是理想的.后一种方法更适合只有一个选择器的场景.
Alternatively, you can handle the Loaded event for the ListPicker itself and set the binding there. The previous approach is ideal if you have multiple pickers. The latter approach is more suited to the scenario when you just have a single picker.
注意:我没有检查 Toolkit 的最新源代码,看看这是否仍然是一个突出的错误.
NOTE: I haven't checked the latest source code for the Toolkit to see if this is still an outstanding bug.
这篇关于ListPicker SelectedIndex 在 WP7 中未正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论