C#更改backgroundcolor特定行

use*_*083 11 c# silverlight wpf xaml

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

<!--
    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>
Run Code Online (Sandbox Code Playgroud)

我想从第0行(包含页面标题)更改backgroundcolor.有任何想法吗??提前致谢!

这一行构成:

    <!-- 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>
Run Code Online (Sandbox Code Playgroud)

Dut*_*tts 17

您无法在Grid.Row本身上设置背景颜色,而是在占用此行的任何内容上设置Background属性.

例如

<Grid Style="{StaticResource LayoutRootStyle}">
  <Grid.RowDefinitions>
    <RowDefinition Height="140"/>
    <RowDefinition Height="*"/>
  </Grid.RowDefinitions>
  <Grid Background="Red" Grid.Row="0">
    <TextBlock>Test</TextBlock>
  </Grid>
</Grid>
Run Code Online (Sandbox Code Playgroud)

编辑:更新了Silverlight; TextBlock没有Background,因此您必须将控件放在另一个具有背景的Border或grid容器中.代码已更新以反映此情况.