none
XamlParseException: Operation is not valid while ItemsSource is in use

    Question

  • So I'm still learning WPF with the "WPF Pro C# in 2008" book and online resources.  However, I've come across something that I'm having a hard time understanding how to fix.

     

    The full error message:

    'GridView' object cannot be added to 'ListView'. Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead

     

    Here's the gist of the most basic version of my code I can create that shows the problem:

    ================================================================

    <Window x:Class="ListView_issue.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:JLB="clr-namespace:ListView_issue"

    Title="Window1" Height="317" Width="390">

    <Window.Resources>

         <CollectionViewSource x:Key="Pending" Source="{Binding JLB:Window1.PendingApprovalCases}" />

    <CollectionViewSource x:Key="Rejected" Source="{Binding Source={x:Static Application.Current}, Path=RejectedCases}" />

    </Window.Resources>

    <StackPanel>

    <ListView ItemsSource="{Binding Source={StaticResource Pending}}">

         <GridView>

              <GridView.Columns>

                   <GridViewColumn Header="Office" DisplayMemberBinding="{Binding Path=Office}" />

              </GridView.Columns>

         </GridView>

    </ListView>

    <ListView ItemsSource="{Binding Source={StaticResource Rejected}}">

         <GridView>

              <GridView.Columns>

                   <GridViewColumn Header="Office" DisplayMemberBinding="{Binding Path=Office}" />

              </GridView.Columns>

         </GridView>

    </ListView>

    </StackPanel>

    </Window>

     

    public partial class App : Application {

    private ObservableCollection<Case> _pending_approval_cases = new ObservableCollection<Case>();

    private ObservableCollection<Case> _rejected_cases = new ObservableCollection<Case>();

    public App() { }

    public ObservableCollection<Case> PendingApprovalCases {

         get { return _pending_approval_cases; }

    }

    public ObservableCollection<Case> RejectedCases {

         get { return _rejected_cases; }

    }

     

    public class Case {

    public string Office = string.Empty;

    public Case(string value) { Office = value; }

    }

    ================================================================

     

    If I comment out the second ListView in the xaml, the program gets past the Window1's

    "InitializeComponent()", otherwise I get the Exception above.  So there must be something being shared between the two ListViews that it doesn't like.  Not sure if I need to create separate Observable classes for each type (Pending & Rejected) instead of just using the one class with two collections.

     

    I'm still working at it trying to figure it out, but any help would be appreciated.  Maybe where my collection is being held (app instead of window)?

     

    Jeff

    Sunday, March 16, 2008 9:51 AM

Answers

  • Your markup is not correct...   If you want to use a GridView as the view for your ListView, you must set it as the ListView.View property, as shown here:

     

    Code Snippet

     

    <ListView ItemsSource="{Binding Source={StaticResource Pending}}">

       <ListView.View>

         <GridView>

              <GridView.Columns>

                   <GridViewColumn Header="Office"

                       DisplayMemberBinding="{Binding Path=Office}" />

              </GridView.Columns>

         </GridView>

      </ListView.View>

    </ListView>

     

     

     

    In your markup, the parser will try to add the GridView to the Items collection within the ListView.  This is not valid because you are using the ListView in ItemsSource mode, as the error mentions.

    Sunday, March 16, 2008 12:22 PM