微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xaml – 绑定ListPicker.SelectedIndex问题

我正在尝试在 Windows Phone 7 UserControl中对ListPicker的Selectedindex属性进行双向绑定.

当我设置DataContext时,它引发以下异常:
Selectedindex必须始终设置为有效值.

这是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的数据.

我看不出问题出在哪里……

在ItemsSource解决问题后移动Selectedindex.

这是工作片段

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

有人对此有解释吗?

原文地址:https://www.jb51.cc/windows/365342.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐