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

Ala*_*aor 10 c# silverlight wpf linq-to-sql windows-phone-7

我在Windows Phone 7应用程序中有一个页面,用户可以在其中编辑或删除Transaction对象.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" />
Run Code Online (Sandbox Code Playgroud)

ListPicker_Tap事件是针对Windows Phone的WPF Toolkit的Aug/2011版本中的错误的修复,简单地说是:

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

如果用户编辑事务,一切都很好,但如果用户尝试删除它,我会收到一条错误,指出"SelectedItem必须始终设置为有效值".

如果用户单击TransactionPage.xaml.cs中appbar中的delete按钮,则代码如下:

    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();
    }
Run Code Online (Sandbox Code Playgroud)

我的ViewModel.DeleteTransaction方法:

    public void DeleteTransaction(Transaction transaction)
    {
        AllTransactions.Remove(transaction);
        transactionRepository.Delete(transaction);
    }
Run Code Online (Sandbox Code Playgroud)

我的transactionRepository.Delete方法:

    public void Delete(Transaction transaction)
    {
        Context.Transactions.DeleteOnSubmit(transaction);
        Context.SubmitChanges();
    }
Run Code Online (Sandbox Code Playgroud)

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

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

在propertyName属性中,值为"Category".看起来当删除对象时发送类别和帐户的propertychanged事件,并且因为listpicker处于TwoWay模式,所以在处理它时遇到一些麻烦.我该怎么办呢?我需要一些帮助.

Ric*_*ter 12

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

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

<toolkit:ListPicker DisplayMemberPath="Title" SelectionMode="Single" 
SelectedItem="{Binding SelectedCategory, Mode=TwoWay}"
ItemsSource="{Binding Categories}" />
Run Code Online (Sandbox Code Playgroud)

这有效,因为itemssource首先被初始化:

<toolkit:ListPicker DisplayMemberPath="Title" SelectionMode="Single" 
ItemsSource="{Binding Categories}"
SelectedItem="{Binding SelectedCategory, Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)


Pra*_*ian 1

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

另外,如果您定义处理程序的原因是由于放置在 中时无法打开的Tap错误,请查看补丁 ID 10247。如果您使用该补丁重新编译工具包,问题就会得到解决。ListPickerScrollViewer