Listpicker 错误 SelectedItem 必须始终设置为有效值 [英] Listpicker error SelectedItem must always be set to a valid value

查看:16
本文介绍了Listpicker 错误 SelectedItem 必须始终设置为有效值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a page in a Windows Phone 7 app where the user can edit or delete an Transaction object. The Transaction object is an Linq-to-Sql class that have a relationship with the Account class and the Category class. In the page, I use a ListPicker to let the user select the account and category for the given transaction, like this:

<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 事件修复了适用于 Windows Phone 的 WPF 工具包 2011 年 8 月版本中的错误,简单来说就是:

The ListPicker_Tap event is a fix for a bug in the Aug/2011 version of the WPF Toolkit for Windows Phone and is simply this:

    private void ListPicker_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        ListPicker lp = (ListPicker)sender; 
        lp.Open();
    }

如果用户编辑交易,一切正常,但如果用户尝试删除它,我会收到一条错误消息,提示SelectedItem 必须始终设置为有效值".

If the user edit the transaction, everything is fine, but if the user try to delete it, I get an error saying that "SelectedItem must always be set to a valid value".

如果用户单击 TransactionPage.xaml.cs 中应用栏中的删除按钮,则代码如下:

Here's the code if the user click in the delete button in the appbar in the TransactionPage.xaml.cs:

    private void appBarDelete_Click(object sender, EventArgs e)
    {
        MessageBoxResult result = MessageBox.Show("Are you sure?
", "Confirm", MessageBoxButton.OKCancel);

        if (result == MessageBoxResult.OK)
        {
            App.ViewModel.DeleteTransaction(transaction);
        }

        NavigationService.GoBack();
    }

我的 ViewModel.DeleteTransaction 方法:

My ViewModel.DeleteTransaction method:

    public void DeleteTransaction(Transaction transaction)
    {
        AllTransactions.Remove(transaction);
        transactionRepository.Delete(transaction);
    }

我的 transactionRepository.Delete 方法:

My transactionRepository.Delete method:

    public void Delete(Transaction transaction)
    {
        Context.Transactions.DeleteOnSubmit(transaction);
        Context.SubmitChanges();
    }

我在 Context.SubmitChanges() 执行中收到错误,调试指向 Transaction 类中的 NotifyPropertyChanged,我收到错误的行是这样的:

I receive the error in the Context.SubmitChanges() execution, the debug points to the NotifyPropertyChanged inside the Transaction class, the line where I get the error is this:

    protected virtual void SendPropertyChanged(String propertyName)
    {
        if ((this.PropertyChanged != null))
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

在 propertyName 属性中,值为类别".貌似删除对象时发送category和accounts的propertychanged事件,由于listpicker是TwoWay模式,处理起来有些麻烦.我怎么能修好呢?我需要一些帮助.

In the propertyName attribute the value is "Category". It looks like when deleting the object send the propertychanged event of category and accounts, and because the listpicker is in the TwoWay mode, it have some trouble dealing with it. How could I fix it? I need some help.

推荐答案

问题是 ListPicker 期望 SelectedItemListPickerItem 而您将它绑定到 Transaction 类型的对象.您可以通过绑定到 SelectedIndex 属性来解决这个问题,然后根据索引从您的 ViewModel 中选择适当的对象.

The problem is that the ListPicker is expecting the SelectedItem to be a ListPickerItem whereas you're binding it to an object of type Transaction. You can get around the problem by binding to the SelectedIndex property instead and then select the appropriate object from your ViewModel based on the index.

此外,如果您定义了 Tap 处理程序的原因是因为 ListPicker 在放置在 ScrollViewer 中时无法打开的错误>,查看补丁 ID 10247.如果您使用该补丁重新编译工具包,它就会解决问题.

Also, if the reason you have the Tap handler defined is because of the bug where the ListPicker does not open when placed within a ScrollViewer, take a look at patch ID 10247. If you recompile the toolkit with that patch it fixes the problem.

这篇关于Listpicker 错误 SelectedItem 必须始终设置为有效值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆