2013-03-12 179 views
11

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

<!-- 
    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行(其中包含页面标题)更改backgroundcolor。 任何想法??提前致谢!的

该行consits:

<!-- 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> 

回答

17

你不能设置在Grid.Row本身的背景色,而不是设置任何占据此行的背景属性。

例如

<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没有背景,所以你必须把控件放在另一个有背景的边框或网格容器中。代码更新以反映这一点。

+0

不幸的是文本块项目不包含背景属性的边界。我编辑了我的帖子以显示哪些项目在该行中。 – user1951083 2013-03-12 08:33:07

+0

啊,我的道歉,我没有看到你的帖子上的Silverlight标签,WPF TextBlock有背景。 – Dutts 2013-03-12 08:36:25

+0

但我不想改变按钮的背景,它必须是整行。 – user1951083 2013-03-12 08:36:28

2

如何将在你需要它

<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> 

请注意,您可以E指定列的情况下跨越包括多个列