Click here to Skip to main content
15,794,593 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a master-detail user interface. Optionally, the user should be able to populate some of the fields with default values.
So I say

void button1_Click(object sender, RoutedEventArgs e)
       {
       textBox1.Text = "6545" // this is fine
           listView1.Items.Add(new { name = "test" , value = "fifty" }); // this throws an exception
       }
Exception is:

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.


????? Why can't I mimic in code the input of data via the GUI?

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

Relevant portions of the code-behind are:

private void Window_Loaded(object sender, RoutedEventArgs e) {

        this.PostData = db.Posts;

        MasterViewSource = (CollectionViewSource)this.FindResource("MasterView");
            DetailViewSource = (CollectionViewSource)this.FindResource("DetailView");
        MasterView = (BindingListCollectionView)this.MasterViewSource.View;
            MasterView.CurrentChanged += new EventHandler(MasterView_CurrentChanged);
            DetailView = (BindingListCollectionView)this.DetailViewSource.View;
        //.................///

    }
----------


My xaml has


<CollectionViewSource x:Key="MasterView" />
  <CollectionViewSource Source="{Binding Source={StaticResource MasterView}, Path='PostDetails'}" x:Key="DetailView" />

  <!--  .................... -->

 <ListView Name="listView1" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Source={StaticResource DetailView}}">
     <!--  ............. -->
    <ListView.View>
        <GridView>
            <!--  .............. -->
            <GridViewColumn>
                            <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                            <ComboBox ItemsSource="{Binding Source={StaticResource NameLookup}}"SelectedValue="{Binding Path=AccountName}" DisplayMemberPath="name"                                         SelectedValuePath="name" />
                                         </DataTemplate>
                </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                 </GridView>
         </ListView.View>
 </ListView>
-------


Thanks
Posted
Updated 24-Apr-11 11:14am
v2

1 solution

You can't do this because you are actually using a data binding, rather than directly assigning values from the start. You have two choices here, either add the items to the collection you have bound to (which should implement INotifyCollectionChanged - ObservableCollection is a good choice here), or you need to use a variation of the following:
C#
ICollectionView view = (ICollectionView)CollectionViewSource.GetDefaultView(listView1.ItemsSource);
var item = view.CurrentItem;
item.Add(....);
There you go, that should help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900