How to create following styles in wpf? - 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

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 discrete color brush?

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?

Extracting Text from WPF

i have some xaml files that include text, this text is in paterns, like this paths and canvas
i need a way to convert them to row text; thank you
<Path Data="M603.0815,627.8994C590.7485,627.8994,580.7505,617.9014,580.7505,605.5674C580.7505,593.2354,590.7485,583.2374,603.0815,583.2374C615.4145,583.2374,625.4115,593.2354,625.4115,605.5674C625.4115,617.9014,615.4145,627.8994,603.0815,627.8994z" Height="45.677" Canvas.Left="504.149" StrokeStartLineCap="Flat" Stretch="Fill" StrokeEndLineCap="Flat" Stroke="White" StrokeThickness="1.015" StrokeMiterLimit="4" StrokeLineJoin="Miter" Canvas.Top="482.064" Width="45.676"/>
<Canvas Height="45.311" Canvas.Left="500.553" Opacity="0.7599949836730957" Canvas.Top="478.508" Width="45.309">
<Canvas Height="45.311" Canvas.Left="0" Canvas.Top="0" Width="45.309">
<Canvas.Clip>
<PathGeometry Figures="M584.42,624.485 C574.056,614.12 574.055,597.314 584.42,586.948 C594.786,576.583 611.591,576.583 621.956,586.948 z" FillRule="Nonzero">
<PathGeometry.Transform>
<MatrixTransform Matrix="1,0,0,1,-576.646606445313,-579.174255371094"/>
</PathGeometry.Transform>
</PathGeometry>
</Canvas.Clip>
<Path Data="F1M584.42,624.485C574.056,614.12,574.055,597.314,584.42,586.948C594.786,576.583,611.591,576.583,621.956,586.948z" Height="45.311" Canvas.Left="0" Stretch="Fill" Canvas.Top="0" Width="45.309">
<Path.Fill>
<LinearGradientBrush EndPoint="0.562,0.679" StartPoint="0.454,0.215">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Path.Fill>
<Path.OpacityMask>
<LinearGradientBrush EndPoint="0.562,0.679" StartPoint="0.454,0.215">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="sc#0.6782913, 0, 0, 0" Offset="0.080963134765625"/>
<GradientStop Color="sc#0.4013089, 0, 0, 0" Offset="0.18010848760604858"/>
<GradientStop Color="sc#0.178047657, 0, 0, 0" Offset="0.31294554471969604"/>
<GradientStop Color="sc#0.02753514, 0, 0, 0" Offset="0.54220128059387207"/>
<GradientStop Color="sc#0, 0, 0, 0" Offset="1"/>
<GradientStop Color="sc#0, 0, 0, 0" Offset="1"/>
</LinearGradientBrush>
</Path.OpacityMask>
</Path>
</Canvas>
</Canvas>
<Path Data="F1M232.805,421.572C229.604,421.572,226.683,422.892,224.564,425.017L224.564,425.017C222.448,427.145,221.128,430.081,221.128,433.312L221.128,433.312L221.128,439.382L221.128,472.247C221.128,475.478,222.446,478.415,224.566,480.544L224.566,480.544C226.683,482.667,229.599,483.987,232.807,483.987L232.807,483.987L327.873,483.987C331.076,483.987,333.995,482.667,336.113,480.544L336.113,480.544C338.232,478.415,339.548,475.478,339.548,472.247L339.548,472.247L339.548,453.323L339.548,447.284L339.548,433.312C339.548,430.081,338.233,427.145,336.111,425.017L336.111,425.017C333.995,422.892,331.078,421.572,327.873,421.572L327.873,421.572z M222.652,460.445L222.652,433.312C222.652,430.513,223.792,427.968,225.632,426.124L225.632,426.124C227.471,424.275,230.009,423.128,232.805,423.128L232.805,423.128L327.873,423.128C330.667,423.128,333.206,424.275,335.048,426.124L335.048,426.124C336.883,427.968,338.026,430.513,338.026,433.312L338.026,433.312L338.026,447.284L338.024,453.323L338.024,472.247C338.024,475.046,336.884,477.591,335.047,479.434L335.047,479.434C333.206,481.285,330.667,482.43,327.873,482.43L327.873,482.43L232.807,482.43C230.011,482.43,227.471,481.285,225.63,479.434L225.63,479.434C223.792,477.591,222.652,475.046,222.652,472.247L222.652,472.247z" Height="62.415" Canvas.Left="145.034" Stretch="Fill" Canvas.Top="320.906" Width="118.42">
<Path.Fill>
<LinearGradientBrush EndPoint="0.5,0" StartPoint="0.5,1">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="White" Offset="0.080001801252365112"/>
<GradientStop Color="#FF878687" Offset="0.69325298070907593"/>
<GradientStop Color="sc#1, 0.307234675, 0.303684026, 0.307234675" Offset="0.88050458675388654"/>
<GradientStop Color="sc#1, 0.448453128, 0.445626259, 0.448453128" Offset="0.92941428201599052"/>
<GradientStop Color="sc#1, 0.6357934, 0.6339267, 0.6357934" Offset="0.96216528915308075"/>
<GradientStop Color="sc#1, 0.859842, 0.8591236, 0.859842" Offset="0.98765014041117993"/>
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Path.Fill>
</Path>
<Canvas Height="57.274" Canvas.Left="147.674" Opacity="0.5" Canvas.Top="323.477" Width="113.02">
<Canvas Height="57.274" Canvas.Left="0" Opacity="0.38999900221824646" Canvas.Top="0" Width="113.02">
<Path Data="M336.788,456.73L336.788,434.419C336.788,428.765,332.133,424.143,326.449,424.143L320.519,424.143L305.105,424.143L234.111,424.143C228.425,424.143,223.769,428.765,223.769,434.419L223.769,456.73L223.768,439.382L223.768,471.14C223.768,476.795,228.422,481.417,234.107,481.417L240.037,481.417L255.451,481.417L326.444,481.417C332.128,481.417,336.783,476.795,336.783,471.14L336.783,439.382z" Height="57.274" Canvas.Left="0" Stretch="Fill" Canvas.Top="0" Width="113.02">
<Path.Fill>
<LinearGradientBrush EndPoint="-0.039,0.5" StartPoint="1.213,0.5">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#FFC5C5C5" Offset="0.69325298070907593"/>
<GradientStop Color="sc#1, 0.5962005, 0.5962005, 0.5962005" Offset="0.88050458675388654"/>
<GradientStop Color="sc#1, 0.678514, 0.678514, 0.678514" Offset="0.92941428201599052"/>
<GradientStop Color="sc#1, 0.787711, 0.787711, 0.787711" Offset="0.96216528915308075"/>
<GradientStop Color="sc#1, 0.9183046, 0.9183046, 0.9183046" Offset="0.98765014041117993"/>
<GradientStop Color="White" Offset="1"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Path.Fill>
</Path>
</Canvas>
</Canvas>
<Canvas Height="46.997" Canvas.Left="147.674" Canvas.Top="323.477" Width="113.02">
<Canvas.Clip>
<PathGeometry Figures="M320.519,424.143 L305.105,424.143 L234.112,424.143 C228.425,424.143 223.769,428.765 223.769,434.419 L223.769,434.419 L223.769,456.731 L223.768,439.382 L223.768,471.14 C243.672,458.167 336.788,434.419 336.788,434.419 L336.788,434.419 C336.788,428.765 332.133,424.143 326.449,424.143 L326.449,424.143 z" FillRule="Nonzero">
<PathGeometry.Transform>
<MatrixTransform Matrix="1,0,0,1,-223.768005371094,-424.143005371094"/>
</PathGeometry.Transform>
</PathGeometry>
</Canvas.Clip>
<Path Data="F1M320.519,424.143L305.105,424.143L234.112,424.143C228.425,424.143,223.769,428.765,223.769,434.419L223.769,434.419L223.769,456.731L223.768,439.382L223.768,471.14C243.672,458.167,336.788,434.419,336.788,434.419L336.788,434.419C336.788,428.765,332.133,424.143,326.449,424.143L326.449,424.143z" Height="46.997" Canvas.Left="0" Stretch="Fill" Canvas.Top="0" Width="113.02">
<Path.Fill>
<LinearGradientBrush EndPoint="-0.039,0.5" StartPoint="1.213,0.5">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Path.Fill>
<Path.OpacityMask>
<LinearGradientBrush EndPoint="-0.039,0.5" StartPoint="1.213,0.5">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="sc#0.5, 0, 0, 0" Offset="0.22740978002548218"/>
<GradientStop Color="sc#0, 0, 0, 0" Offset="1"/>
</LinearGradientBrush>
</Path.OpacityMask>
</Path>
</Canvas>
Look how it is done in the Microsoft Sample Application called locbaml.
You might want to consider an XmlDataProvider together with a suitable XPath, or an XslTransform like
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="text()">
<xsl:copy />
</xsl:template>
</xsl:stylesheet>

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.

Resources