本文介绍了在DataGrid单元格中添加一个彩色的小矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望在DataGrid中添加一个微小的(10x10)矩形作为单元格.我已经在对象中设置了它,我只是在寻找一种将其从代码中获取到我的DataGrid中的方法.
I am looking to add a tiny (10x10) rectangle as a cell in my DataGrid. I already have it set in the object I'm just looking for a way to get it from the code into my DataGrid.
这是我的DataGrid XAML:
This is my 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>
请查看评论,以更轻松地了解我想要的内容.
Please see the comment for an easier understanding of what I want.
推荐答案
用 DataGridTemplateColumn
替换 DataGridTextColumn
.像这样:
<DataGridTemplateColumn >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Rectangle Width="10" Height="10" Fill="{Binding TemplateCellColour}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
是的, TemplateCellColour
应该是 Brush
,这是正确的.
And yes, TemplateCellColour
should be a Brush
, that is correct.
这篇关于在DataGrid单元格中添加一个彩色的小矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!