加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

xaml – 绑定ListPicker.SelectedIndex问题

发布时间:2020-12-14 04:10:05 所属栏目:Windows 来源:网络整理
导读:我正在尝试在 Windows Phone 7 UserControl中对ListPicker的SelectedIndex属性进行双向绑定. 当我设置DataContext时,它引发以下异常: SelectedIndex必须始终设置为有效值. 这是XAML代码 Grid x:Name="LayoutRoot" Grid.RowDefinitions RowDefinition Height
我正在尝试在 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}"/>

有人对此有解释吗?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读