gpt4 book ai didi

c# - Listpicker 错误 SelectedItem 必须始终设置为有效值

转载 作者:可可西里 更新时间:2023-11-01 09:00:30 26 4
gpt4 key购买 nike

我在 Windows Phone 7 应用程序中有一个页面,用户可以在其中编辑或删除事务对象。 Transaction 对象是一个 Linq-to-Sql 类,它与 Account 类和 Category 类有关系。在该页面中,我使用 ListPicker 让用户为给定交易选择帐户和类别,如下所示:

<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 年 8 月版的 Windows Phone WPF 工具包中的一个错误,简单来说就是:

    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”。貌似删除对象的时候发送了category和accounts的propertychanged事件,因为listpicker是TwoWay模式,处理起来有些麻烦。我该如何解决?我需要一些帮助。

最佳答案

此错误也可能是由 XAML 属性的顺序引起的:

这不起作用(抛出异常,因为在设置 SelectedItem 时 ItemsSource 为 null):

<toolkit:ListPicker DisplayMemberPath="Title" SelectionMode="Single" 
SelectedItem="{Binding SelectedCategory, Mode=TwoWay}"
ItemsSource="{Binding Categories}" />

这是因为 itemssource 首先被初始化:

<toolkit:ListPicker DisplayMemberPath="Title" SelectionMode="Single" 
ItemsSource="{Binding Categories}"
SelectedItem="{Binding SelectedCategory, Mode=TwoWay}" />

关于c# - Listpicker 错误 SelectedItem 必须始终设置为有效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7719682/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com