开发者

Applying a background color to an entire Grid row in XAML Silverlight

开发者 https://www.devze.com 2023-01-22 02:29 出处:网络
I\'m trying to apply a gradient background to just one row in a XAML Silverlight grid that I\'ve created.

I'm trying to apply a gradient background to just one row in a XAML Silverlight grid that I've created.

I can do something like this without any trouble:

<Grid>
    <Grid.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black" Offset="0" />
            <GradientStop Color="White" Offset="1" />
        </LinearGradientBrush>
    </Grid.Background>

    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>开发者_JAVA技巧
        <RowDefinition/>
    </Grid.RowDefinitions>

    <!-- components and various stuffs -->
</Grid>

Unfortunately this applies the gradient to the entire grid.

It seems as though I can't apply a gradient (or even a color) to an individual row definition in the grid. Is it possible?

Thanks!


Use a Border, and then use Grid.Row and Grid.ColumnSpan to put it in the specific row of the Grid that you want.

<Grid>
   <Grid.ColumnDefinitions>
       <ColumnDefinition/>
       <ColumnDefinition/>
   </Grid.ColumnDefinitions>
   <Grid.RowDefinitions>
       <RowDefinition/>
       <RowDefinition/>
   </Grid.RowDefinitions>

   <Border Grid.Row="1" Grid.ColumnSpan="2">
      <Border.Background>
         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="Black" Offset="0" />
            <GradientStop Color="White" Offset="1" />
         </LinearGradientBrush>
      </Border.Background>
   </Border>

   <!-- other controls in the grid -->
</Grid>


If you want to simulate two colors in vertical order, you can also use the Offset property. If you set these two on the same value, you get this simulation:

<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Green" Offset="0.366" />
                <GradientStop Color="Red" Offset="0.366" />
            </LinearGradientBrush>
</Border.Background>

You can do even more :)

<Border.Background>
   <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
     <GradientStop Color="Green" Offset="0.3" />
     <GradientStop Color="Red" Offset="0.3" />
     <GradientStop Color="Red" Offset="0.7" />
    <GradientStop Color="Yellow" Offset="0.7" />
    <GradientStop Color="Yellow" Offset="0.9" />
    <GradientStop Color="Blue" Offset="0.9" />
  </LinearGradientBrush>
</Border.Background>

0

精彩评论

暂无评论...
验证码 换一张
取 消