Silverlight LinearGradientBrush Color not correct - silverlight

I want to use LinearGradientBrush to horizontal fill my Rectangle on canvas.
My code:
<Rectangle Width="200" Height="200">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5" >
<GradientStop Color="Red" Offset="0" />
<GradientStop Color="Blue" Offset="1" />
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
But I notice that the left-most color of red is not #FFFF0000, and it was #FFFE0000
So did anyone know why this happen?

Gradient is drawn using interpolation, so it tends to the color you choose but it does not need to draw it exactly. It also depends on the size of the element for which you use the gradient.
Workaround - set Offset property as below:
<Rectangle Width="200" Height="200">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="Red" Offset="0.01" />
<GradientStop Color="Blue" Offset="0.99"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>

Related

WPF background colors

I would like to create some colors as my WPF background, but I have 2 issues with that:
When I resize windows, coloring being resized with it. I would like to have it static.
My colors are specific: They are not toned, they should look something like this:
But the problem is: When I create a gradient in WPF, color changes are kind of blurred.
I have tried to do it that way :
<Window.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,0">
<GradientStop Color="#70116B" Offset="0" />
<GradientStop Color="#70116B" Offset="0.4" />
<GradientStop Color="#BBD909" Offset="0.4" />
<GradientStop Color="#BBD909" Offset="0.5" />
<GradientStop Color="#0093DD" Offset="0.5" />
<GradientStop Color="#0093DD" Offset="0.52" />
<GradientStop Color="White" Offset="0.52" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Window.Background>
But like I said: it's not what I want
What you want to achieve isn't a gradient. But i would suggest you to use something like below:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.4*"/>
<ColumnDefinition Width="0.1*"/>
<ColumnDefinition Width="0.02*"/>
<ColumnDefinition Width="0.48*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" Background="#70116B"/>
<Grid Grid.Column="1" Background="#BBD909"/>
<Grid Grid.Column="2" Background="#0093DD"/>
<Grid Grid.Column="3" Background="#FFFFFF"/>
</Grid>
Hope this helps!

WPF - Change grid backcolor on MouseLeftButtonDown

I have a Grid that I am essentially using as a button which I need to change the backcolor gradient to another gradient while the mouse button is down. When the mouse button is released, change it back to the original color. Additionally, I will need to perform some action as well. I am trying to accomplish this in the code behind but maybe this can be done in the xaml? I am going about it this way as customizing a button to the look and feel that I needed was proving to be more difficult. How can I go about this?
XAML:
<DockPanel LastChildFill="True" Width="40" Height="40" Margin="22,20,22,5">
<Border BorderThickness="0,1,0,0" DockPanel.Dock="Top" BorderBrush ="#747474" />
<Border BorderThickness="1,0,0,0" DockPanel.Dock="Left">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#747474" Offset="0"/>
<GradientStop Color="#464648" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
<Border BorderThickness="0,0,1,0" DockPanel.Dock="Right">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#747474" Offset="0"/>
<GradientStop Color="#464648" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
</Border>
<Border BorderThickness="0,1,0,0" DockPanel.Dock="Bottom" BorderBrush ="#464648" />
<Grid Width="38" Height="38">
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#585858" Offset="0"/>
<GradientStop Color="#464648" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="/common/printer_20.png" Stretch="None" />
</Grid>
You have several ways to achieve this.
Use a Style and Visual States. Problem: It would be dirty executing a command.
Add a Behavior to the Grid. Assign the Mouse Events and change Background in code. Additionally you can execute a Command.
Use a Trigger to change the background. Use MVVM Light to execute a Command.
Which way would you prefer?

Background image over gradient

I have an image with a transparent background which I would like to overlay on my window as an image behind all of the controls.
My window background already has a gradient brush, but what I can tell by Googling and experimenting, you cannot have two background brushes.
Here is the XAML which isn't working. What do you suggest? Maybe there is another way of setting the image.
<Window.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFF7F7F7"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
<ImageBrush ImageSource="/Images/Arrow.png">
</ImageBrush>
</Window.Background>
You may put an Image control into a top-level Grid, below all other controls:
<Grid>
<Grid.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFF7F7F7"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Image Source="/Images/Arrow.png">
<Grid>
... all other controls go here
</Grid>
</Grid>

Is there any way to display Horizontal OR Vertical lines in Rectangle

Is there any way without using image to show horizontal or vertical lines in the Rectangle.
Yes you can easily do this with a LinearGradientBrush:
<Rectangle Width="100" Height="100">
<Rectangle.Fill>
<LinearGradientBrush SpreadMethod="Reflect" StartPoint="0 0" EndPoint="0 0.05">
<GradientStop Offset="0.5" Color="Black"/>
<GradientStop Offset="0.5" Color="White"/>
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
You control line thickness and orientation with the EndPoint property.

How to style a fancy vertical ProgressBar correctly?

I'm trying to make a stylish progress bar, but I'm having a problem with its vertical version. A picture is worth a thousand words:
http://img402.imageshack.us/img402/2033/progressq.gif
Everything I've attempted so far has resulted in Wrong. How do I achieve Right? I'd prefer a XAML only solution unless it's slow or causes flickering when the progress bar is updated many times per second.
Here's one alternative:
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="3" Padding="3">
<Grid Width="20" Height="100">
<Grid Height="{Binding ProgressValue}" VerticalAlignment="Bottom">
<Grid Height="100" VerticalAlignment="Bottom">
<Grid.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="Yellow" Offset="0.0" />
<GradientStop Color="Red" Offset="0.25" />
<GradientStop Color="Blue" Offset="0.75" />
<GradientStop Color="LimeGreen" Offset="1.0" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
</Grid>
</Grid>
</Border>
Note the third line from the top, that is where you bind your progress value.

Resources