System.InvalidOperationException: 'colección de elementos debe estar vacío antes de usar ItemsSource.'

votos
0

Espero que alguien pueda mirar esto con un par de ojos frescos ya que no puedo verlo.

El error que consigo es: System.InvalidOperationException: 'colección de elementos debe estar vacío antes de usar ItemsSource'

Otros puestos aquí en StackOverflow sugieren que mantener un ojo en GridViewColumn y GridViewColumn.CellTemplate así como la DataTemplate. Parece ser que mira correcta y esperando que usted puede ver algo yo no.

A continuación se muestra el C #. El mensaje de error que aparece más arriba en realidad apunta a la .ItemsSource abajo - pero necesito esto:

private void RefreshList()
    {
        lstoList.ItemsSource = oList;
        oList.Clear();
        foreach (var offVar in db.O)
        {
            oList.Add(offVar);
        }
        lstoList.Items.Refresh();
    }

A continuación se muestra el código XAML:

<Page x:Class=LApplication.p
  xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
  xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
  xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 
  xmlns:d=http://schemas.microsoft.com/expression/blend/2008 
  xmlns:local=clr-namespace:LApplication
  mc:Ignorable=d 
  Loaded=Page_Loaded
  d:DesignHeight=500 d:DesignWidth=900
  Title=p>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height=200 />
        <RowDefinition Height=* />
        <RowDefinition Height=* />
    </Grid.RowDefinitions>

    <!--<StackPanel>-->
        <TabControl Height=200>
            <TabItem Header=L Applicants>
                <Grid>
                    <Grid.ColumnDefinitions>

                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height=50*/>
                        <RowDefinition Height=*/>
                        <RowDefinition Height=*/>
                        <RowDefinition Height=*/>
                    </Grid.RowDefinitions>

                    <ListView Name=lstoList Grid.Row=0 Grid.ColumnSpan=1 SelectionChanged=lstoList_SelectionChanged>
                        <ListView.View>
                                        <GridView>
                                            <GridViewColumn Header=UserId Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding UserId} ></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header=QuoteId Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding QuoteId}></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>
                                            <GridViewColumn Header=oAmount Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding oAmount}></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header=Term Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding Term} ></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header=IRate Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding IRate}></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header=FirstName Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding FirstName}></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header=LastName Width=120 >
                                                    <GridViewColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding LastName}></TextBlock>
                                                        </DataTemplate>
                                                    </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header=datetime Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding datetime}></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header=oStatusId Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding oStatusId}></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header=pName Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding pName}></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header=CompanyReg Width=120 >
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding CompanyReg}></TextBlock>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>
                                        </GridView> 
                        </ListView.View>

                        <!--Enables right click-->
                        <ListView.ContextMenu>
                            <ContextMenu Name=UserListOverview IsEnabled=True>
                                <ContextMenu.Items>
                                    <MenuItem Header=Add New o Name=submenuAddo IsEnabled=True Click=submenuAdd_Click>
                                        <MenuItem.Icon>
                                            <Image Source=Images/add.png Stretch=Uniform></Image>
                                        </MenuItem.Icon>
                                    </MenuItem>
                                    <MenuItem Header=Update Selected o Name=submenuModifyo IsEnabled=True >
                                        <MenuItem.Icon>
                                            <Image Source=Images/update.png Stretch=Uniform></Image>
                                        </MenuItem.Icon>

                                    </MenuItem>

                                    <MenuItem Header=Remove Selected o Name=submenuDeleteo IsEnabled=True>
                                        <MenuItem.Icon>
                                            <Image Source=Images/remove.png Stretch=Uniform></Image>
                                        </MenuItem.Icon>
                                    </MenuItem>
                                </ContextMenu.Items>

                            </ContextMenu>

                        </ListView.ContextMenu>
                        <ScrollBar x:Name=scrollBar Height=100 Width=500/>
                    </ListView>

                </Grid>
            </TabItem>

            <TabItem Header=p Logs>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width=1200*/>
                        <ColumnDefinition Width=*/>
                        <ColumnDefinition Width=*/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height=400/>
                        <RowDefinition Height=50*/>
                    </Grid.RowDefinitions>

                    <ListView Name=lstpList Grid.Row=0 Grid.ColumnSpan=2>
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header=UserId Width=120 >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding UserId} ></TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                <GridViewColumn Header=datetime Width=120 >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding datetime} ></TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                <GridViewColumn Header=oAmount Width=120 >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding oAmount}></TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                <GridViewColumn Header=Term Width=120 >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding Term}></TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                <GridViewColumn Header=Action Width=120 >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding Action}></TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                <GridViewColumn Header=AffIntRate Width=120 >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding AffIntRate}></TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                <GridViewColumn Header=RpmtIntRate Width=120 >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding RpmtIntRate}></TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                                <GridViewColumn Header=oStatusId Width=120 >
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBlock HorizontalAlignment=Stretch TextWrapping=Wrap Text={Binding oStatusId}></TextBlock>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>

                            </GridView>
                        </ListView.View>
                    </ListView>
                </Grid>
            </TabItem>
            <ScrollBar x:Name=scrollBar1 Height=13 Width=358/>
        </TabControl>
    <!--</StackPanel>-->


    <!--users and Logs tabs-->
     <StackPanel Name=stkpDetails Grid.RowSpan=4 Height=400 VerticalAlignment=Top Grid.Row=1 Grid.Column=1 Width=400 Visibility=Visible HorizontalAlignment=Center>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width=40*/>
                    <ColumnDefinition Width=60*/>

                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height=10*/>
                    <RowDefinition Height=*/>
                    <RowDefinition Height=*/>
                    <RowDefinition Height=*/>
                    <RowDefinition Height=*/>
                    <RowDefinition Height=*/>
                    <RowDefinition Height=*/>
                    <RowDefinition Height=*/>
                    <RowDefinition Height=*/>
                    <RowDefinition Height=*/>
                    <RowDefinition Height=*/>
                </Grid.RowDefinitions>
                <!---No need to add names as they will not have any interaction-->
                <Label Grid.Row=0 Grid.Column=0 Content=UserId />
                <Label Grid.Row=1 Grid.Column=0 Content=QuoteId />
                <Label Grid.Row=2 Grid.Column=0 Content=oAmount />
                <Label Grid.Row=3 Grid.Column=0 Content=oTerm />
                <Label Grid.Row=4 Grid.Column=0 Content=oIntRate />
                <Label Grid.Row=5 Grid.Column=0 Content=p Name />
                <Label Grid.Row=8 Grid.Column=0 Content=o Status />

                <TextBox Grid.Row=0 Grid.Column=1 Name=tbxUserId />
                <TextBox Grid.Row=1 Grid.Column=1 Name=tbxQuoteId />
                <TextBox Grid.Row=2 Grid.Column=1 Name=tbxoAmount />
                <TextBox Grid.Row=3 Grid.Column=1 Name=tbxoTerm />
                <TextBox Grid.Row=4 Grid.Column=1 Name=tbxoIntRate />
                <TextBox Grid.Row=5 Grid.Column=1 Name=tbxpName />

                <ComboBox Name=cbooStatus Grid.Column=1 Grid.Row=8>
                        <ComboBoxItem Content=Please select/>
                        <ComboBoxItem Content=Approved/>
                        <ComboBoxItem Content=Rejected />
                        <ComboBoxItem Content=Pending />
                        <ComboBoxItem Content=Revised o />
                </ComboBox>

                    <Button Name=btnUpdate Grid.Row=9 Grid.Column=2 Content=Update Width=60 Height=30 HorizontalAlignment=Center Click=btnUpdate_Click></Button>
            </Grid>
        </StackPanel>
</Grid>

Publicado el 18/12/2018 a las 11:09
fuente por usuario
En otros idiomas...                            

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more