How to create discrete color brush? - wpf

To have brush:
I'm currently write this code:
<LinearGradientBrush EndPoint="1,0">
<GradientStop Offset="{Binding Path=offset1}" Color="{Binding Path=g1}"/>
<GradientStop Offset="{Binding Path=offset1}" Color="{Binding Path=g2}"/> <!-- -->
<GradientStop Offset="{Binding Path=offset2}" Color="{Binding Path=g2}"/> <!-- -->
<GradientStop Offset="{Binding Path=offset2}" Color="{Binding Path=g3}"/>
</LinearGradientBrush>
That's what a kind of cheat! I'm wondering is there is a better way to do this?

Related

wpf gradient brush like the picture [duplicate]

I currently have a gradient set up in XAML as follows:
<Canvas>
<Canvas.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="Red" Offset="0.5"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
</Canvas>
Result:
However, this is not what I want to achieve. What I want is to have one gradient transition to a solid colour. Below is what I am trying to achieve:
As one can see from the above image, there is a white-red gradient at the top, and that transitions smoothly to black as it goes down. I have yet to find a way to do this, and this is where I need help. Is there a way to do this? If there is, what is the best way to achieve this (that you know of)?
You may overlay a second element with a perpendicular LinearGradientBrush that changes opacity:
<Grid>
<Canvas>
<Canvas.Background>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="Red" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
</Canvas>
<Canvas>
<Canvas.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#00000000" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
</Canvas>
</Grid>

How do I make a multi-directional gradient in WPF?

I currently have a gradient set up in XAML as follows:
<Canvas>
<Canvas.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="Red" Offset="0.5"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
</Canvas>
Result:
However, this is not what I want to achieve. What I want is to have one gradient transition to a solid colour. Below is what I am trying to achieve:
As one can see from the above image, there is a white-red gradient at the top, and that transitions smoothly to black as it goes down. I have yet to find a way to do this, and this is where I need help. Is there a way to do this? If there is, what is the best way to achieve this (that you know of)?
You may overlay a second element with a perpendicular LinearGradientBrush that changes opacity:
<Grid>
<Canvas>
<Canvas.Background>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="Red" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
</Canvas>
<Canvas>
<Canvas.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#00000000" Offset="0"/>
<GradientStop Color="Black" Offset="1"/>
</LinearGradientBrush>
</Canvas.Background>
</Canvas>
</Grid>

How to create following styles in wpf?

How do you style the button contents as shown below? For style 1, seems easy to do by using text blocks and Run, but style 2, I'm wondering how to achieve this.
You can do this with offset
<TextBlock FontSize="72">
<Run Foreground="Gray" Text="It" /><Run Text="e">
<Run.Foreground>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="Gray" Offset="0"/>
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="Red" Offset="0.5" />
<GradientStop Color="Red" Offset="1" />
</LinearGradientBrush>
</Run.Foreground>
</Run><Run Text="m" Foreground="Red" /><Run Text=" 1">
<Run.Foreground>
<LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
<GradientStop Color="Red" Offset="0"/>
<GradientStop Color="Red" Offset="0.5" />
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="Gray" Offset="1" />
</LinearGradientBrush>
</Run.Foreground>
</Run>
</TextBlock>
Ref: Foreground colour of textblock based on position of part of the letter

Border with Linear Gradient fill doesn't show border at run time

Here is my XAML
<Border Grid.Column="1" BorderBrush="Black" Margin="5">
<Border.Background>
<LinearGradientBrush EndPoint=".5,1" StartPoint=".5,0">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#FFf3f3f3" Offset="0.48"/>
<GradientStop Color="#FFededed" Offset="0.51"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
</Border>
I can see border at design time but it doesn't show at run-time. Why?
Whats wrong with it?!
You need to set the BorderThickness.

WPF Background Two Tone

I'm trying to do something so simple but cannot get anywhere with it. I've been trawling the net trying to find a clear example or tit bit of information but every article I find is showing the exact same simple example.
All I want to do is have a background to a list box item that is two tone but without a gradial blend between them. So far I have:
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#ACC6E0" Offset="0"/>
<GradientStop Color="#DCE7F5" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
I've tried plenty of different things but all I end up with is a variant of a gradual gradient not a two tone equal split.
Many Thanks
Paul
Just add two stops at the same offset:
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#ACC6E0" Offset="0"/>
<GradientStop Color="#ACC6E0" Offset="0.5"/>
<GradientStop Color="#DCE7F5" Offset="0.5"/>
<GradientStop Color="#DCE7F5" Offset="1"/>
</LinearGradientBrush>
In fact you can drop the end points:
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#ACC6E0" Offset="0.5"/>
<GradientStop Color="#DCE7F5" Offset="0.5"/>
</LinearGradientBrush>
You need to add extra GradientStops:
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="#ACC6E0" Offset="0"/>
<GradientStop Color="#ACC6E0" Offset="0.5"/>
<GradientStop Color="#DCE7F5" Offset="0.5"/>
<GradientStop Color="#DCE7F5" Offset="1"/>
</LinearGradientBrush>

Resources