c#更改背景色特定行

本文关键字:背景色 | 更新日期: 2023-09-27 18:03:22

我已经从Grid App (XAML)模板(c# Windows Store)创建了一个新项目。到目前为止,我在模板中没有更改任何内容,但我想更改网格中特定行的背景色。

<!--
    This grid acts as a root panel for the page that defines two rows:
    * Row 0 contains the back button and page title
    * Row 1 contains the rest of the page layout
-->
<Grid Style="{StaticResource LayoutRootStyle}">
    <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

我想改变第0行(包含页面标题)的背景色。有什么想法?提前感谢!

这一行包含:

    <!-- Back button and page title -->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
        <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Grid.Column="1" IsHitTestVisible="false" Style="{StaticResource PageHeaderTextStyle}"/>
    </Grid>

c#更改背景色特定行

你不能设置网格的背景颜色。行本身,而不是设置背景属性的任何占用这一行。

例如

<Grid Style="{StaticResource LayoutRootStyle}">
  <Grid.RowDefinitions>
    <RowDefinition Height="140"/>
    <RowDefinition Height="*"/>
  </Grid.RowDefinitions>
  <Grid Background="Red" Grid.Row="0">
    <TextBlock>Test</TextBlock>
  </Grid>
</Grid>

编辑:针对Silverlight更新;TextBlock没有背景,所以你必须把控件放在另一个边框或网格容器,它有背景。

如何在需要的地方插入边框

<Grid Style="{StaticResource LayoutRootStyle}">
  <Grid.RowDefinitions>
    <RowDefinition Height="140"/>
    <RowDefinition Height="*"/>
  </Grid.RowDefinitions>
    <Border Background="Red" Grid.ColumnSpan="1"></Border>
    <TextBlock>Test</TextBlock>
    <Border Background="blue" Grid.Row="1" Grid.ColumnSpan="1"></Border>
    <TextBlock Grid.Row="1">Test2</TextBlock>
 </Grid>

注意,在包含更多列的情况下,可以指定列的跨度