1 Replies - 1637 Views - Last Post: 30 December 2016 - 10:18 AM

#1 aidenkael  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 59
  • View blog
  • Posts: 275
  • Joined: 22-October 13

WPF MVVM: Operation is not valid while ItemsSource is in use. Access a

Posted 30 December 2016 - 09:52 AM

I am getting this error Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead. on this line <uc:ClientListUserControl />. Anyone know why?

View:
<Window [i]Things are here but my company would not like me sharing our namespaces with you :P/>/>/>[/i]>
    <window.DataContext>
        <vm:ClientListViewModel />
    </window.DataContext>
    <Grid>
        <StackPanel>
            <ItemsControl ItemsSource="{Binding Clients}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <uc:ClientListUserControl></uc:ClientListUserControl>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>        </StackPanel>
    </Grid>
</Window>



ViewModel:
namespace My.Namespace
{
    public class ClientListViewModel
    {
        public ObservableCollection<ClientModel> Clients { get; set; }

        public ClientListViewModel()
        {
            #region DebugAndTesting

            Clients = new ObservableCollection<ClientModel>() {new ClientModel() {ClientId = 1, ClientName = "Company", ClientNumber = 1234 }, new ClientModel() { ClientId = 2, ClientName = "Another Company", ClientNumber = 5678} };

            #endregion
        }   
    }
}



User Control:
<UserControl 
......>
    <Grid>
        <Button Content="{Binding ClientName}" Command="{Binding LoadPreferencesCommand}"  Margin="1,5,1,5"></Button>
    </Grid>
</UserControl>


This post has been edited by aidenkael: 30 December 2016 - 10:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: WPF MVVM: Operation is not valid while ItemsSource is in use. Access a

#2 aidenkael  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 59
  • View blog
  • Posts: 275
  • Joined: 22-October 13

Re: WPF MVVM: Operation is not valid while ItemsSource is in use. Access a

Posted 30 December 2016 - 10:18 AM

Figured it out. I was nesting this view inside of a Shellwindow.xaml. The error was occurring because I had two windows nested inside of each other. Changing my view from a window to a usercontrol worked. Working code below:

View:
<UserControl [i]Things are here but my company would not like me sharing our namespaces with you :P/>/>/>/>[/i]>
    <UserControl.DataContext>
        <vm:ClientListViewModel />
    </UserControl.DataContext>
    <Grid>
        <StackPanel>
            <ItemsControl ItemsSource="{Binding Clients}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Content="{Binding ClientName}" Margin="1,10,1,10" Command="OpenClientPreferenceCommand" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>        
</StackPanel>
    </Grid>
</Window>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1