我在Windows Phone 7应用程序中有一个页面,用户可以在其中编辑或删除一个Transaction对象。 Transaction对象是一个与Account类和Category类有关系的Linq-to-Sql类。在该页面中,我使用ListPicker让用户选择指定交易的帐户和类别,如:Listpicker错误SelectedItem必须始终设置为有效值
<toolkit:ListPicker Grid.Row="1" FullModeHeader="Choose the Account" FullModeItemTemplate="{StaticResource FullModeItemTemplate}" ExpansionMode="FullScreenOnly" Background="#26000000" Margin="10,0,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,10,0" Name="Category" SelectedItem="{Binding Category, Mode=TwoWay}" Tap="ListPicker_Tap" />
的ListPicker_Tap事件是在八月/ 2011版的WPF的一个bug修复工具包的Windows Phone和很简单:
private void ListPicker_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ListPicker lp = (ListPicker)sender;
lp.Open();
}
如果用户编辑的交易,一切都很好,但如果用户试图删除它,我得到一个错误说“的SelectedItem必须始终设置为有效值“。
下面的代码,如果用户点击删除按钮在appbar在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中收到错误()执行,调试点,交易类中的NotifyPropertyChanged,在那里我得到了错误的路线是这样的:
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
在propertyName的属性值是“类别”。它看起来像是在删除对象时发送类别和帐户的属性更改事件,并且由于listpicker处于双向模式,所以在处理它时遇到了一些麻烦。我怎么修复它?我需要一些帮助。
您可以将完整的XAML?或者如果它太长,只要我们可以看到'{Binding Account,Mode = TwoWay}'和'{Binding Category,Mode = TwoWay}'指的是什么,部分XAML就可以。 – Amry