如何通过MyToolkit自定义UWP DataGrid?

时间:2019-06-22 17:39:47

标签: c# uwp datagrid

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

colored grid

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

1 个答案:

答案 0 :(得分:0)

有一种简单的方法可以满足您的要求。您只需要为DataGridTemplateColumn.CellTemplate定义DataTemplate并将背景绑定到自定义类中的某个属性即可。

请参阅我的以下简单代码示例:

<controls:DataGrid
AutoGenerateColumns="False"
ItemsSource="{Binding tests}" GridLinesVisibility="All">
        <controls:DataGrid.Columns>
            <!-- Name Column -->
            <controls:DataGridTemplateColumn Header="Name">
                <controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"  Background="{Binding Color1}">
                            <TextBlock
                        Text="{Binding Name}" />
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplateColumn.CellTemplate>
            </controls:DataGridTemplateColumn>
            <controls:DataGridTemplateColumn Header="Name">
                <controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"  Background="{Binding Color1}">
                            <TextBlock
                        Text="{Binding Name}" />
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplateColumn.CellTemplate>
            </controls:DataGridTemplateColumn>
            <controls:DataGridTemplateColumn Header="Name">
                <controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"  Background="{Binding Color1}">
                            <TextBlock
                        Text="{Binding Name}" />
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplateColumn.CellTemplate>
            </controls:DataGridTemplateColumn>
            <controls:DataGridTemplateColumn Header="Name">
                <controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"  Background="{Binding Color2}">
                            <TextBlock
                        Text="{Binding Name}" />
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplateColumn.CellTemplate>
            </controls:DataGridTemplateColumn>
            <controls:DataGridTemplateColumn Header="Name">
                <controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"  Background="{Binding Color2}">
                            <TextBlock
                        Text="{Binding Name}" />
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplateColumn.CellTemplate>
            </controls:DataGridTemplateColumn>
            <controls:DataGridTemplateColumn Header="Name">
                <controls:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal"  Background="{Binding Color2}">
                            <TextBlock
                        Text="{Binding Name}" />
                        </StackPanel>
                    </DataTemplate>
                </controls:DataGridTemplateColumn.CellTemplate>
            </controls:DataGridTemplateColumn>
        </controls:DataGrid.Columns>
    </controls:DataGrid>
public sealed partial class MainPage : Page
{
    public ObservableCollection<Test> tests { get; set; }

    public MainPage()
    {
        this.InitializeComponent();
        tests = new ObservableCollection<Test>();
        for (int i = 0; i < 15; i++)
        {
            if (i < 7)
            {
                tests.Add(new Test() { Name = "Name " + i, Color1 = new SolidColorBrush(Colors.Yellow), Color2 = new SolidColorBrush(Colors.Red) });
            }
            else
            {
                tests.Add(new Test() { Name = "Name " + i, Color1 = new SolidColorBrush(Colors.Green), Color2 = new SolidColorBrush(Colors.LightBlue) });
            }
        }
        this.DataContext = this;
    }
}

public class Test
{
    public string Name { get; set; }
    public SolidColorBrush Color1 { get; set; }
    public SolidColorBrush Color2 { get; set; }
}