xaml - 绑定(bind) ListPicker.SelectedIndex 问题

标签 xaml windows-phone-7 binding listpicker

我正在尝试在 Windows Phone 7 UserControl 中对 ListPicker 的 SelectedIndex 属性进行双向绑定(bind)。

当我设置 DataContext 时,它会引发以下异常:SelectedIndex must always be set to a valid value.
这是 XAML 代码







<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <toolkit:ListPicker
        Grid.Row="0"
        x:Name="List1"
        SelectionChanged="Picker_SelectionChanged"
        SelectedIndex="{Binding PickerSelectedIndex, Mode=TwoWay}"
        ItemTemplate="{StaticResource PickerTemplate}"
        ItemsSource="{Binding MyList}"/>
</Grid>

以及 DataContext 中的代码
    private ObservableCollection<MyClass> myList = null;
    public ObservableCollection<MyClass> MyList
    {
        get { return this.myList; }
        set
        {
            if (value != this.myList)
            {
                this.myList= value;
                NotifyPropertyChanged("MyList");

                this.PickerSelectedIndex = 0;
            }
        }
    }

    private int pickerSelectedIndex = 0;
    public int PickerSelectedIndex
    {
        get
        {
            return this.pickerSelectedIndex;
        }
        set
        {
            this.pickerSelectedIndex= value;
        }
    }

PickerSelectedIndex.get 中设置断点我可以看到它被正确返回( 0 )。
我确定问题是SelectedIndex="{Binding PickerSelectedIndex, Mode=TwoWay}"因为删除这一行可以解决问题,并且我可以看到 ListPicker 正确加载了 MyList 中的数据。

看不出哪里有问题...

最佳答案

搬家SelectedIndexItemsSource 之后解决了这个问题。

这是工作片段

<toolkit:ListPicker
    Grid.Row="0"
    x:Name="List1"
    SelectionChanged="Picker_SelectionChanged"
    ItemTemplate="{StaticResource PickerTemplate}"
    ItemsSource="{Binding MyList}"
    SelectedIndex="{Binding PickerSelectedIndex, Mode=TwoWay}"/>

有人对此有解释吗?

关于xaml - 绑定(bind) ListPicker.SelectedIndex 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6032882/

相关文章:

windows - 在 WinRT XAML 中的 GridView 项目组内垂直滚动

windows-phone-7 - 将麦克风流保存为 mp3 或 wave

windows-phone-7 - 在 windows phone 8 中将图像转换为灰度

asp.net-mvc - 程序集绑定(bind)重定向不起作用

c# - 使用 RelativeSource 绑定(bind)到父对象属性

WPF 内联样式未设置模板值

c# - 如何取消订阅 MvvmCross 中的 WeakSubscribe

c# - WPF 如何根据 treeviewitem 类型更改上下文菜单项?

windows-phone-7 - 管理 WMAppManifest 文件中列出的应用程序 'Capabilities' 的正确方法是什么?

c++ - SqlQuery 一个命名占位符多次