错误:在使用ItemsSource之前,项集合必须为空。数据网格

时间:2015-12-17 13:20:21

标签: wpf datagrid

我不知道为什么xml代码会发生这种错误。

它说:

  

"在使用ItemsSource"

之前,项目集合必须为空

当我添加DataGridTextColumn.CellStyle触发器部分时会发生这种情况,否则它会正常工作。我需要帮助。

我的XAML代码是:

<Window x:Class="WPFDatagridCustomization.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loc="clr-namespace:WPFDatagridCustomization"
    xmlns:local="clr-namespace:WPFDatagridCustomization.HelperClasses"
    Title="MainWindow"
    Width="600"
    Height="500">

<Grid>
    <DataGrid Name="dataGrid1"
              Grid.Row="3"
              Grid.Column="1"
              Margin="10"
              HorizontalAlignment="Left"
              VerticalAlignment="Top"
              AlternationCount="2"
              AutoGenerateColumns="False"
              ItemsSource="{Binding TemperatureCollection}"
              Loaded="dataGrid1_Loaded"
              MinRowHeight="26"

              RowDetailsVisibilityChanged="dataGrid1_RowDetailsVisibilityChanged">



        <DataGrid.Columns>



            <DataGridTextColumn Width="120"
                                Binding="{Binding TableNo}"
                                Header="Table No" />
            <DataGridTextColumn Width="120"
                                Binding="{Binding TableArea}"
                                Header="Unknown Column" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time6}"
                                Header="6:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time630}"
                                Header="6:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time7}"
                                Header="7:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time730}"
                                Header="7:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time8}"
                                Header="8:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time830}"
                                Header="8:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time9}"
                                Header="9:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time930}"
                                Header="9:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time10}"
                                Header="10:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1030}"
                                Header="10:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time11}"
                                Header="11:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1130}"
                                Header="11:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time12}"
                                Header="12:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1230}"
                                Header="12:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time13}"
                                Header="13:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1330}"
                                Header="13:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time14}"
                                Header="14:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1430}"
                                Header="14:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time15}"
                                Header="15:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1530}"
                                Header="15:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time16}"
                                Header="16:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1630}"
                                Header="16:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time17}"
                                Header="17:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1730}"
                                Header="17:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time18}"
                                Header="18:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1830}"
                                Header="18:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time19}"
                                Header="19:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time1930}"
                                Header="19:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time20}"
                                Header="20:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time2030}"
                                Header="20:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time21}"
                                Header="21:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time2130}"
                                Header="21:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time22}"
                                Header="22:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time2230}"
                                Header="22:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time23}"
                                Header="23:00" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time2330}"
                                Header="23:30" />
            <DataGridTextColumn Width="40"
                                Binding="{Binding time24}"
                                Header="24:00" />


        </DataGrid.Columns>

        <DataGridTextColumn>
            <DataGridTextColumn.CellStyle>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="Text" Value="1">
                            <Setter Property="Background" Value="LightGreen"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>         

       </DataGrid>
     </Grid>
 </Window>

1 个答案:

答案 0 :(得分:1)

您在DataGrid-Node的正下方添加了最后一个DataGridTextColumn,这就是它被视为项目的原因。

将它放在DataGrid.Columns下面,你会没事的。