Mic*_*ulo 8 xaml binding windows-phone-7 listpicker
我正在尝试在Windows Phone 7 UserControl中对ListPicker的SelectedIndex属性进行双向绑定.
当我设置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>
Run Code Online (Sandbox Code Playgroud)
和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;
}
}
Run Code Online (Sandbox Code Playgroud)
在PickerSelectedIndex.get我的断点中可以看到它正确返回(0).我确信问题是SelectedIndex="{Binding PickerSelectedIndex, Mode=TwoWay}"因为删除此行可以解决问题,我可以看到ListPicker正确加载了来自MyList的数据.
我看不出问题出在哪里......
移动SelectedIndex后ItemsSource解决了问题.
这是工作片段
<toolkit:ListPicker
Grid.Row="0"
x:Name="List1"
SelectionChanged="Picker_SelectionChanged"
ItemTemplate="{StaticResource PickerTemplate}"
ItemsSource="{Binding MyList}"
SelectedIndex="{Binding PickerSelectedIndex, Mode=TwoWay}"/>
Run Code Online (Sandbox Code Playgroud)
有人对此有解释吗?
| 归档时间: |
|
| 查看次数: |
9099 次 |
| 最近记录: |