System.InvalidOperationException:'在使用ItemsSource之前,Items集合必须为空。

时间:2018-12-18 11:09:43

标签: wpf

我希望有人能看到一双新鲜的眼睛,因为我看不到它。

我得到的错误是:System.InvalidOperationException:'在使用ItemsSource之前,Items集合必须为空。'

StackOverflow上的其他文章建议密切关注GridViewColumn和GridViewColumn.CellTemplate以及DataTemplate。它看起来似乎正确,希望您能看到我看不到的东西。

下面是C#。上面提供的错误消息实际上指向下面的.ItemsSource-但我需要这样做:

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

下面是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>

0 个答案:

没有答案