如何为自定义表格设置单元格背景颜色?

8

我有:

<CustomControl:GridControl ShowCustomGridLines="True" Grid.Column="2" Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
        <TextBlock Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Name="txbCaption" Text="{Binding Caption}" />
            <CustomControl:GridControl ShowCustomGridLines="True" Grid.Column="2" Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                </Grid.RowDefinitions>
                <TextBlock Grid.Column="0" Grid.Row="1" Text="П" HorizontalAlignment="Center" VerticalAlignment="Center" />
                <TextBlock Grid.Column="1" Grid.Row="1" Text="Ф" HorizontalAlignment="Center" VerticalAlignment="Center" Background="Yellow" />
                <TextBlock Grid.Column="2" Grid.Row="1" Text="%" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </CustomControl:GridControl>

我想设置一个文本框的单元格背景颜色(其中Background="Yellow")。设置文本框的背景并不能解决问题,因为我需要设置整个单元格的背景颜色,即使没有文本也要如此。
请问如何实现?
4个回答

13

您可以在该区域中放置某种类型的面板并设置其背景颜色。例如:

<Rectangle Fill="Black" IsHitTestVisible="False" Grid.Column="1" Grid.Row="1"/>

6

或者在单元格中添加一个边框,然后将所需控件放置在边框内并设置边框的背景颜色属性。

<Border Grid.Column="0" Grid.Row="0" Background="#FF3C3C3F">
     <TextBlock>Some Text</TextBlock>
</Border>

6

要为整个行或列设置背景颜色,在行或列定义中添加以下内容:

<Grid Grid.Row="0" Grid.Column="0" Background="SomeColor"/>

您可以通过行+列的方式指定单元格位置,然后在任何您想要的地方插入文本框。

1
WPF网格不知道什么是“单元格”。在其中放置一个面板并设置其颜色。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接