wpf C# 更改背景颜色特定行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15356273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:01:23  来源:igfitidea点击:

C# Change backgroundcolor specific row

c#wpfsilverlightxaml

提问by user1951083

I've created a new project from the Grid App (XAML) template (C# Windows Store). So far I've changed nothing in the template, but I would like to change the backgroundcolor from a specific row in the grid.

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

I would like to change the backgroundcolor from row 0 (which contains the page title). Any ideas?? Thanks in advance!

我想从第 0 行(包含页面标题)更改背景颜色。有任何想法吗??提前致谢!

This row consits of:

该行包括:

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

回答by Dutts

You can't set the background color on the Grid.Row itself, instead set the Background property on whatever occupies this row.

您不能在 Grid.Row 本身上设置背景颜色,而是在占据该行的任何内容上设置背景属性。

For example

例如

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

EDIT: Updated for Silverlight; TextBlock doesn't have Background so you have to put the control in another Border or grid container which does have background. Code updated to reflect this.

编辑:为 Silverlight 更新;TextBlock 没有背景,因此您必须将控件放在另一个有背景的边框或网格容器中。代码更新以反映这一点。

回答by i31nGo

How about inserting border where you need it

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

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

note that you can especify the columns span in case of include more columns

请注意,如果包含更多列,您可以指定列跨度