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

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

问题描述

我在Windows Phone 7的应用程序,用户可以编辑或删除事务对象有一个页面。该交易对象是一个LINQ到SQL类具有与账户类和类别类的关系。在该页面中,我使用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事件是在八月/ 2011版的WPF工具包的Windows的一个bug修复电话很简单:

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".

下面的代码,如果用户点击在删除按钮appbar在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?\n", "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()执行,调试点该交易类中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的属性值是类别。它看起来删除发送对象类别和账户的PropertyChanged事件的时候就好了,因为listpicker是在双向模式下,它有一些麻烦和它打交道。我怎么能解决这个问题?我需要一些帮助。

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 是期待的SelectedItem 是一个 ListPickerItem ,而你绑定到类型的对象交易。您可以通过,而不是绑定到的SelectedIndex 属性来获取解决问题,然后从基于索引的视图模型合适的对象。

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.

此外,如果原因,您必须定义的点击的处理程序是由于错误的地方 ListPicker 当放置在不打开的ScrollViewer ,看一看的补丁编号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天全站免登陆