C# Change backgroundcolor specific row

19,049

Solution 1

You can't set the background color on the Grid.Row itself, instead set the Background property on whatever occupies this 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.

Solution 2

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

Share:
19,049
user1951083
Author by

user1951083

Updated on June 04, 2022

Comments

  • user1951083
    user1951083 almost 2 years

    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.

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

    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>
    
  • user1951083
    user1951083 about 11 years
    unfortunately a textblock item does not contain of a background property. I've edited my post to show which items are in that row.
  • Dutts
    Dutts about 11 years
    Ah, my apologies, I didn't see the silverlight tag on your post, the WPF TextBlock has Background.
  • user1951083
    user1951083 about 11 years
    But I don't want to change the background of just the button, it has to be the entire row.
  • Dutts
    Dutts about 11 years
    I've updated my answer accordingly, you have to wrap your controls in a Border or Grid (something which does have a Background property)
  • user1951083
    user1951083 about 11 years
    <Grid Grid.Row="0" Background="#26111D"></Grid> Did the trick, thank you!
  • Dutts
    Dutts about 11 years
    No worries mate, glad to help =)
  • user1951083
    user1951083 about 11 years
    Just wondering if I could do it in the StandardStyles.xaml file so I don't have to edit each page. In all my pages I call the StaticResource LayoutRootStyle. <Style x:Key="LayoutRootStyle" TargetType="Panel"> <Setter Property="Background" Value="#FFDDF2AE" /> <Setter Property="ChildrenTransitions"> <Setter.Value> <TransitionCollection> <EntranceThemeTransition/> </TransitionCollection> </Setter.Value> </Setter> </Style> I've allready set a background property for the entire body.
  • Dutts
    Dutts about 11 years
    I'm not entirely sure, what happens if you create a style to set the Background with a TargetType="Grid" ?