Home > Software design >  WPF TextBlock: Stretch Gradient over the entire block vs Line by Line
WPF TextBlock: Stretch Gradient over the entire block vs Line by Line

Time:02-07

The control is a WPF TextBlock, The intent here is to use a gradient to color the entire paragraph from top to bottom vs the default behavior of stretching the gradient over each line in the paragraph.

Any ideas as to how to achieve that? The image below shows the intent.

This is a visual of the intent

LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
myLinearGradientBrush.StartPoint = new Point(0,0);
myLinearGradientBrush.EndPoint = new Point(1,1);
myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.LimeGreen, 1.0));

TextBlock ReportsTextBlock = new TextBlock();
ReportsTextBlock.Foreground = myLinearGradientBrush;

CodePudding user response:

sorry for above post, i misunderstood your post. it seems you want conditional linar gradient brush for each row. long back , i have implemented conditional linar gradient brush. like Train is locked , selected , provisional, cancel , complete , can't complete. I do understand it is textblock control , we can make embed list in textblock and apply conditional LinearGradientBrush , row by row or line by line

Public ReadOnly Property Fill() As LinearGradientBrush Get Dim ret As New LinearGradientBrush Dim gradient As GradientStop

        ret.StartPoint = New Point(0.5, 0)
        ret.EndPoint = New Point(0.5, 1)

        If Train.IsLocked Then
            gradient = New GradientStop
            gradient.Offset = 0
            gradient.Color = Colors.Silver
            ret.GradientStops.Add(gradient)
            gradient = New GradientStop
            gradient.Offset = 0.3
            gradient.Color = Colors.White
            ret.GradientStops.Add(gradient)
            gradient = New GradientStop
            gradient.Offset = 1
            gradient.Color = Colors.Silver
            ret.GradientStops.Add(gradient)

        ElseIf _isSelected Or _train.IsSelected Then
            gradient = New GradientStop
            gradient.Offset = 0
            gradient.Color = Colors.Gold
            ret.GradientStops.Add(gradient)
            gradient = New GradientStop
            gradient.Offset = 0.3
            gradient.Color = Colors.Yellow
            ret.GradientStops.Add(gradient)
            gradient = New GradientStop
            gradient.Offset = 1
            gradient.Color = Colors.Gold
            ret.GradientStops.Add(gradient)

        ElseIf Train.RunningStatus = runningStatus.TERMINATED Then
            ret = BrushFactory.VerticalGradientBrush(Colors.LightGreen)

        ElseIf _isProvisional Then
            Dim c As New Color  'a ligher tone of Colors.Orange, created by increasing the HSL luminosity from 120 to 160.
            c.A = 255
            c.R = 255
            c.G = 195
            c.B = 85
            ret = BrushFactory.VerticalGradientBrush(c)

        ElseIf _isCancelled Then
            gradient = New GradientStop
            gradient.Offset = 0
            gradient.Color = Colors.White
            ret.GradientStops.Add(gradient)

        ElseIf _canComplete Then
            gradient = New GradientStop
            gradient.Offset = 0
            gradient.Color = Colors.CornflowerBlue
            ret.GradientStops.Add(gradient)
            gradient = New GradientStop
            gradient.Offset = 0.3
            gradient.Color = Colors.AliceBlue
            ret.GradientStops.Add(gradient)
            gradient = New GradientStop
            gradient.Offset = 1
            gradient.Color = Colors.CornflowerBlue
            ret.GradientStops.Add(gradient)

        Else    'Cannot complete
            gradient = New GradientStop
            gradient.Offset = 0
            gradient.Color = Colors.LightPink
            ret.GradientStops.Add(gradient)
            gradient = New GradientStop
            gradient.Offset = 0.3
            gradient.Color = Colors.White
            ret.GradientStops.Add(gradient)
            gradient = New GradientStop
            gradient.Offset = 1
            gradient.Color = Colors.LightPink
            ret.GradientStops.Add(gradient)
        End If

        Return ret
    End Get
End Property

CodePudding user response:

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="TextElement.FontFamily" Value="Verdana"/>
    <Setter Property="TextElement.FontSize" Value="11"/>
</Style>

<!-- Style for textblocks with a context menu -->
<Style x:Key="TextWithContextMenu" TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
    <Setter Property="TextDecorations" Value="{StaticResource DecorationForTextWithContextMenu}"/>
</Style>


<TextDecorationCollection x:Key="DecorationForTextWithContextMenu">
          <TextDecoration Location="Underline " PenThicknessUnit="FontRecommended" PenOffset="1">
        <TextDecoration.Pen>
            <Pen Thickness="1" >
                <Pen.Brush>
                    <SolidColorBrush Color="CornflowerBlue" Opacity="0.8"/>
                </Pen.Brush>
                <Pen.DashStyle>
                    <DashStyle Dashes="1 3"/>
                </Pen.DashStyle>
            </Pen>
        </TextDecoration.Pen>
    </TextDecoration>
    <TextDecoration Location="Underline " PenThicknessUnit="FontRecommended" PenOffset="2" >
        <TextDecoration.Pen>
            <Pen Thickness="1" >
                <Pen.Brush>
                    <SolidColorBrush Color="LightBlue" Opacity="0.5"/>
                </Pen.Brush>
                <Pen.DashStyle>
                    <DashStyle Dashes="1 3" Offset="2"/>
                </Pen.DashStyle>
            </Pen>
        </TextDecoration.Pen>
    </TextDecoration>
</TextDecorationCollection>

OR manage with below style

<TextBlock.Foreground>
                        <SolidColorBrush Color="{Binding Brush}" />
                    </TextBlock.Foreground>

I will suggest to manage with style instead of programming

CodePudding user response:

 Public Shared Function GetColorFromHexaDecimalCode(ByVal HcolorCode As String) As System.Windows.Media.Color
    Try
        Return CType(System.Windows.Media.ColorConverter.ConvertFromString(HcolorCode), System.Windows.Media.Color)
    Catch ex As Exception
        Return Windows.Media.Colors.Transparent
    End Try
End Function

Private Function GetForeground() As Brush
    If Not Me.IsEnabled Then
        Return New SolidColorBrush(Helper.GetColorFromHexaDecimalCode("#888"))
    Else
        Return Foreground
    End If
End Function
  •  Tags:  
  • Related