c# – Listpicker错误SelectedItem必须始终设置为有效的值
我在
Windows Phone 7应用程序中有一个页面,用户可以在其中编辑或删除一个Transaction对象. Transaction对象是一个与Account类和Category类有关系的Linq-to-Sql类.在页面中,我使用ListPicker让用户选择给定事务的帐户和类别,如下所示:
<toolkit:ListPicker Grid.Row="1" FullModeHeader="Choose the Account" FullModeItemTemplate="{StaticResource FullModeItemTemplate}" ExpansionMode="FullScreenOnly" Background="#26000000" Margin="10,10,0" Name="Account" SelectedItem="{Binding Account,Mode=TwoWay}" Tap="ListPicker_Tap" /> <toolkit:ListPicker Grid.Row="7" FullModeHeader="Choose the Category" FullModeItemTemplate="{StaticResource FullModeItemTemplate}" ExpansionMode="FullScreenOnly" Background="#26000000" Margin="10,0" Name="Category" SelectedItem="{Binding Category,Mode=TwoWay}" Tap="ListPicker_Tap" /> ListPicker_Tap事件是针对Windows Phone的WP / WPF工具包的8月/ 2011版本的错误的修复,只是这样: private void ListPicker_Tap(object sender,System.Windows.Input.GestureEventArgs e) { ListPicker lp = (ListPicker)sender; lp.Open(); } 如果用户编辑事务,一切都很好,但是如果用户尝试删除它,我会收到一个错误,指出“SelectedItem必须始终被设置为有效的值”. 如果用户点击TransactionPage.xaml.cs中的应用程序栏中的删除按钮,则代码如下: private void appBarDelete_Click(object sender,EventArgs e) { MessageBoxResult result = MessageBox.Show("Are you sure?n","Confirm",MessageBoxButton.OKCancel); if (result == MessageBoxResult.OK) { App.ViewModel.DeleteTransaction(transaction); } NavigationService.GoBack(); } 我的ViewModel.DeleteTransaction方法: public void DeleteTransaction(Transaction transaction) { AllTransactions.Remove(transaction); transactionRepository.Delete(transaction); } 我的transactionRepository.Delete方法: public void Delete(Transaction transaction) { Context.Transactions.DeleteOnSubmit(transaction); Context.SubmitChanges(); } 我在Context.SubmitChanges()执行中收到错误,调试指向Transaction类里面的NotifyPropertyChanged,我得到错误的行是这样的: protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName)); } } 在propertyName属性中,值为“Category”.看起来像删除对象时发送categorychanged的propertychanged事件,并且由于listpicker是TwoWay模式,所以处理它有一些麻烦.我该如何解决?我需要一些帮助. 解决方法
问题是ListPicker希望SelectedItem是一个ListPickerItem,而你将它绑定到一个类型为Transaction的对象.您可以通过绑定到SelectedIndex属性来解决问题,然后根据索引从ViewModel中选择适当的对象.
另外,如果您定义了Tap处理程序的原因是因为ListPicker在放置在ScrollViewer中时不会打开的错误,请查看patch ID 10247.如果您使用该修补程序重新编译工具包,则可以解决问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |