ListPicker SelectedIndex 在 WP7 中未正确显示

发布于 2024-11-03 07:33:59 字数 1499 浏览 2 评论 0原文

我在 WP7 中的 ListPicker 元素遇到了一个奇怪的问题。

问题

通过 Binding 设置 ListPickerSelectedIndex 不会更改加载后显示的默认项目。

示例

作为测试,我快速修改了 Microsoft 的 SettingsSample 以包含 ListPicker。您可以在以下位置下载: http://www.mediafire.com/?w0n0ymkh4dwe9b3

这是我们折叠的ListPicker

 -----------------
| Times New Roman |
 -----------------

这是展开时的 ListPicker

 -----------------
| Times New Roman |
| Arial           |
| Comic Sans MS   |
 -----------------

如果我们选择“Arial”,则导航离开,然后返回再次,ListPicker 仍然显示:

 -----------------
| Times New Roman |
 -----------------

而它应该显示(因为设置已正确保存):

 -----------------
| Arial           |
 -----------------

进一步混乱

在上面的示例中,如果我们单击 ListPicker,实际上选择了“Arial”(因为此 ListBoxItem 的文本被突出显示)。由于某种原因,加载控件时它没有显示为默认值。

如果我们忘记 Binding 方法,而是在 XAML 中手动将 SelectedIndex 设置为“1”,那么在加载控件时我们会得到预期的结果:

 -----------------
| Arial           |
 -----------------

Ideas ?

有没有办法在使用Binding时更新ListPicker控件以正确显示SelectedIndex?我想我错过了一些明显的东西,但我已经搜索过但找不到任何东西。

提前致谢!

I have a strange problem with the ListPicker element in WP7.

The Problem

Setting a ListPicker's SelectedIndex via a Binding doesn't change the default item shown after it loads.

Example

As a test, I've quickly modified Microsoft's SettingsSample to include a ListPicker. You can download it at: http://www.mediafire.com/?w0n0ymkh4dwe9b3

This is our collapsed ListPicker:

 -----------------
| Times New Roman |
 -----------------

And this is ListPicker when it's expanded:

 -----------------
| Times New Roman |
| Arial           |
| Comic Sans MS   |
 -----------------

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

Further Confusion

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.

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

Ideas?

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.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一口甜 2024-11-10 07:33:59

我相信 ListPicker 控件在绑定选择方面存在一个突出的错误。我认为推荐的解决方案是在页面的 OnNavigateTo 覆盖中手动设置绑定。以下代码显示了我如何为我的一个应用程序执行此操作:

        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 事件并在那里设置绑定。如果您有多个选择器,则前一种方法是理想的选择。后一种方法更适合只有一个选择器的情况。

注意:我还没有检查工具包的最新源代码来看看这是否仍然是一个突出的错误。

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);
        }

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.

NOTE: I haven't checked the latest source code for the Toolkit to see if this is still an outstanding bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文