2011-03-29 85 views
6

我期待在我的DataGrid中添加一個細小的(10x10)矩形作爲單元格。我已經把它設置在對象中,我只是想找到一種方法將它從代碼中提取到我的DataGrid中。在DataGrid單元格中添加一個小的彩色矩形

這是我的DataGrid XAML:

<DataGrid Name="dataGrid1" Grid.Row="2" AutoGenerateColumns="False" DataContext="{Binding}" HeadersVisibility="Column" 
       HorizontalGridLinesBrush="#ccc" VerticalGridLinesBrush="#ccc" VirtualizingStackPanel.VirtualizationMode="Standard" Background="#FFF6F6F6" CanUserAddRows="False"> 
     <DataGrid.Resources> 
      <ResourceDictionary Source="Pages/DataGridStyle.xaml" /> 
     </DataGrid.Resources> 
     <DataGrid.Columns> 
      <!-- In here I would like a datagrid cell that is just a 10x10 box which uses {Binding Path=TemplateCellColour} (templatecellcolour is stored as a brush, is this an issue? --> 
      <DataGridTextColumn ElementStyle="{StaticResource CenterTextCell}" Width="0.5*" Binding="{Binding Path=TemplateCellID}" Header="ID"></DataGridTextColumn> 
      <DataGridTextColumn ElementStyle="{StaticResource CenterTextCell}" Width="1*" Binding="{Binding Path=CellWidth}" Header="Width"></DataGridTextColumn> 
      <DataGridTextColumn ElementStyle="{StaticResource CenterTextCell}" Width="1*" Binding="{Binding Path=CellHeight}" Header="Height"></DataGridTextColumn> 
      <DataGridTextColumn ElementStyle="{StaticResource CenterTextCell}" Width="1*" Binding="{Binding Path=CellTop}" Header="Top"></DataGridTextColumn> 
      <DataGridTextColumn ElementStyle="{StaticResource CenterTextCell}" Width="1*" Binding="{Binding Path=CellLeft}" Header="Left"></DataGridTextColumn> 
     </DataGrid.Columns> 
</DataGrid> 

請參閱意見,我想要的東西更容易理解。

回答

5

DataGridTextColumn替換爲DataGridTemplateColumn。事情是這樣的:

<DataGridTemplateColumn > 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <Rectangle Width="10" Height="10" Fill="{Binding TemplateCellColour}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

是的,TemplateCellColour應該是一個Brush,這是正確的。

+0

工作完美,不得不將「背景」更改爲「填充」,但都表現良好。乾杯。 – Prisoner 2011-03-29 15:33:27

+0

@prisoner,很酷,用你的評論更新了答案。從記事本發佈代碼有一些副作用:) – Snowbear 2011-03-29 15:36:26

相關問題