如何从 MyToolkit 自定义 UWP DataGrid?

我正在构建一个 UWP 应用程序,我需要将 DataGrid 分成四个相等的部分,如下所示:

http://img3.mukewang.com/644cda2a00013e0805420288.jpg

但是到目前为止,没有任何方法可以设置单元格的颜色。有没有办法在 C# 中以编程方式执行此操作?

编辑:

经过大量研究,我最终使用 Grid、一堆标签 + 边框和一个虚拟文本框从头开始开发。最后的结果其实还不错:

http://img.mukewang.com/644cda3b0001133e06580302.jpg

红颜莎娜
浏览 129回答 1
1回答

米脂

有一种简单的方法可以满足您的要求。您只需要定义 DataTemplateDataGridTemplateColumn.CellTemplate并将背景绑定到自定义类中的某个属性。请看我下面的简单代码示例:<controls:DataGridAutoGenerateColumns="False"ItemsSource="{Binding tests}" GridLinesVisibility="All">&nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGrid.Columns>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- Name Column -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn Header="Name">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel Orientation="Horizontal"&nbsp; Background="{Binding Color1}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text="{Binding Name}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn Header="Name">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel Orientation="Horizontal"&nbsp; Background="{Binding Color1}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text="{Binding Name}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn Header="Name">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel Orientation="Horizontal"&nbsp; Background="{Binding Color1}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text="{Binding Name}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn Header="Name">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel Orientation="Horizontal"&nbsp; Background="{Binding Color2}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text="{Binding Name}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn Header="Name">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel Orientation="Horizontal"&nbsp; Background="{Binding Color2}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text="{Binding Name}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn Header="Name">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <StackPanel Orientation="Horizontal"&nbsp; Background="{Binding Color2}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TextBlock&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text="{Binding Name}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </StackPanel>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn.CellTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGridTemplateColumn>&nbsp; &nbsp; &nbsp; &nbsp; </controls:DataGrid.Columns>&nbsp; &nbsp; </controls:DataGrid>public sealed partial class MainPage : Page{&nbsp; &nbsp; public ObservableCollection<Test> tests { get; set; }&nbsp; &nbsp; public MainPage()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.InitializeComponent();&nbsp; &nbsp; &nbsp; &nbsp; tests = new ObservableCollection<Test>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < 15; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i < 7)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tests.Add(new Test() { Name = "Name " + i, Color1 = new SolidColorBrush(Colors.Yellow), Color2 = new SolidColorBrush(Colors.Red) });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tests.Add(new Test() { Name = "Name " + i, Color1 = new SolidColorBrush(Colors.Green), Color2 = new SolidColorBrush(Colors.LightBlue) });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; this.DataContext = this;&nbsp; &nbsp; }}public class Test{&nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; public SolidColorBrush Color1 { get; set; }&nbsp; &nbsp; public SolidColorBrush Color2 { get; set; }}
打开App,查看更多内容
随时随地看视频慕课网APP