2015-12-17 45 views
1

我不知道爲什麼這個錯誤繼續發生的XML代碼是。錯誤:使用ItemsSource之前,項目集合必須爲空。數據網格

它說:

"Items collection must be empty before using ItemsSource"

這occures當我否則添加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

您添加的最後一個DataGridTextColumn正下方的數據網格節點,這就是爲什麼它被視爲一個項目。

將它放在DataGrid.Columns下面,你會沒事的。

+0

thankz it worked !!!! –

相關問題