Conditional style in WPF

I created a style that makes a TextBlock look like a link:

 <Style x:Key="linkStyle" TargetType="TextBlock"> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="TextDecorations" Value="Underline" /> </Trigger> </Style.Triggers> <Setter Property="Cursor" Value="Hand" /> <Setter Property="Foreground" Value="Blue" /> <EventSetter Event="MouseLeftButtonDown" Handler="navigateLink" /> </Style> 

How to apply it only when TextBlock.Text starts with http: //?

+4
source share
1 answer

try it

 <Style x:Key="linkStyleConditional" TargetType="{x:Type TextBlock}"> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True"/> <Condition Binding="{Binding Path=Text, Converter={StaticResource SomeConverter}}" Value="True"/> </MultiDataTrigger.Conditions> <Setter Property="Foreground" Value="Orange" /> </MultiDataTrigger> </Style.Triggers> </Style> 

In SomeConverter, write the logic if the text starts with http //: then return true else return false. I hope this helps.

+13
source

Source: https://habr.com/ru/post/1397183/


All Articles