Skip to main content

Microsoft Silverlight

Answered Question GradientStop Offset is always relative, even when the MappingMode is AbsoluteRSS Feed

(0)

shacktoms
shacktoms

Member

Member

185 points

149 Posts

GradientStop Offset is always relative, even when the MappingMode is Absolute

According to the Silverlight documentation, the Offset for a GradientStop is interpreted differently according to the MappingMode of the GradientBrush in which it appears, but this appears to be wrong.

<UserControl x:Class="AbsoluteMapping.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Rectangle Width="400" Height="300">
            <Rectangle.Fill>
                <LinearGradientBrush MappingMode="Absolute" StartPoint="0 0" EndPoint="40 0" SpreadMethod="Repeat">
                    <GradientStop Color="Red" Offset="0"/>
                    <GradientStop Color="Red" Offset=".5"/>
                    <GradientStop Color="Blue" Offset=".5"/>
                    <GradientStop Color="Blue" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</UserControl>

Produces the result that should be produced by...

<UserControl x:Class="AbsoluteMapping.Page"
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <Rectangle Width="400" Height="300">
            <Rectangle.Fill>
                <LinearGradientBrush MappingMode="Absolute" StartPoint="0 0" EndPoint="40 0" SpreadMethod="Repeat">
                    <GradientStop Color="Red" Offset="0"/>
                    <GradientStop Color="Red" Offset="20"/>
                    <GradientStop Color="Blue" Offset="20"/>
                    <GradientStop Color="Blue" Offset="40"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</UserControl>

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Re: GradientStop Offset is always relative, even when the MappingMode is Absolute

Hello, this is the desired behavior. MappingMode only affects Start/EndPoint. It doesn't affect each GradientStop's Offset. This is consistent with WPF.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

shacktoms
shacktoms

Member

Member

185 points

149 Posts

Re: GradientStop Offset is always relative, even when the MappingMode is Absolute

I have no problem with the behavior.  The problem is that the behavior is inconsistent with the documentation.  So if the behavior is right, then the documentation is wrong.

From the documentation (for Offset)...

"If you change the MappingMode to be Absolute, then the Offset is interpreted as pixels within the area where the brush is applied."

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: Re: GradientStop Offset is always relative, even when the MappingMode is Absolute

Can you point the link of the document? This is what I saw from http://msdn2.microsoft.com/en-us/library/system.windows.media.gradientstop.offset(VS.95).aspx:

The MappingMode of a LinearGradientBrush or RadialGradientBrush does not influence how the Offset values are interpreted. Regardless of mapping mode, the offset is always interpreted as a factor between 0 and 1 along the vector that defines the gradient.

 

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

shacktoms
shacktoms

Member

Member

185 points

149 Posts

Re: Re: GradientStop Offset is always relative, even when the MappingMode is Absolute

Interesting... that is the exact opposite of what I saw.  Looks like the online documentation has already been fixed.

I was looking in the Silverlight Documentation (March 2008) that I downloaded in accord with the instructions in the Welcome page of the Silverlight 2.0 SDK.   In the exact same place in the documentation hierarchy, it says this (Remarks section, everything that follows is Microsoft documentation)...

The MappingMode of a LinearGradientBrush or RadialGradientBrush influences how the Offset values are interpreted. Typically you use the default mapping mode, which interprets the offsets as being a relative factor of the total area where the brush is applied. A value of 0 specifies that the stop is positioned at the beginning of the gradient vector, while a value of 1.0 specifies that the stop is positioned at the end of the gradient vector. For a LinearGradientBrush the 0 offset specifies its StartPoint. For a RadialGradientBrush the 0 offset specifies its GradientOrigin. This gradient stop offset factor is then mapped onto how each type of brush specifies its vector. A LinearGradientBrush vector follows a line that runs from its StartPoint to its EndPoint. A RadialGradientBrush vector radiates from its GradientOrigin towards the ellipse formed by Center, RadiusX and RadiusY.

Generally an Offset value is provided between 0 and 1.0 when using the default MappingMode. Values slightly above 1 or slightly below 0 will influence the gradient for this mode and are valid, but the same resulting gradient could be achieved entirely using values between 0 and 1 if all the gradient stops are adjusted relatively.

If you change the MappingMode to be Absolute, then the Offset is interpreted as pixels within the area where the brush is applied.

For examples and illustrations, see Brushes (Silverlight 2).

 

shacktoms
shacktoms

Member

Member

185 points

149 Posts

Re: Re: GradientStop Offset is always relative, even when the MappingMode is Absolute

The current CHM file is still out of date.   If you go to Silverlight.net, click on Get Started, Silverlight 2 Beta 1 SDK, go down the page to Instructions, where it says "If you are interested in installing the Silverlight SDK Standalone Documentation, please go here."   Click the link, it should take you here...

http://www.microsoft.com/downloads/details.aspx?FamilyId=1840CAB5-196C-4264-B55D-562242A72625&displaylang=en

But it looks like it is fixed now online, so I assume it will be updated everywhere eventually, and I have no problems.

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities