Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Effect in WPF

Tags:

c#

wpf

xaml

effects

I'm using WPF. Is there any way to get an effect like this one:

enter image description here

LineEffect

So basically a gradient with multiple lines on top. The number lines should increase based on the width/height of the element.


1 Answers

I would use two layers, first rect is background and second is that are overlapping

<!-- Background gradient -->
<Rectangle Width="200" Height="100">
    <Rectangle.Fill>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF5B5B5B" Offset="0.008"/>
            <GradientStop Color="#FFA6A6A6" Offset="1"/>
        </LinearGradientBrush>
    </Rectangle.Fill>    
</Rectangle>

<!-- Lines layer -->
<Rectangle Width="200" Height="100">
    <Rectangle.Fill>
        <VisualBrush
                TileMode="Tile"
                Viewport="0,0,7,7"
                ViewportUnits="Absolute"
                Viewbox="0,0,7,7"
                ViewboxUnits="Absolute" >
            <VisualBrush.Visual>
                <Line X1="7" X2="0" Y1="0" Y2="7" Stroke="Gray" />
            </VisualBrush.Visual>
        </VisualBrush>
    </Rectangle.Fill>
</Rectangle>

In response to @Shlomo

You could eventually change brush to contain two lines instead of one to get rid of spacing when zoomed-in. The solution would look something like this:

<VisualBrush.Visual>
    <Grid>
        <Line X1="10" X2="0" Y1="0" Y2="10" Stroke="Gray" />
        <Line X1="4" X2="-1" Y1="-1" Y2="4" Stroke="Gray" />
    </Grid>
</VisualBrush.Visual>

In this way we don't need those ugly approximated numbers.

like image 200
Aleksandar Toplek Avatar answered Jun 24 '26 12:06

Aleksandar Toplek