Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional style in WPF

Tags:

c#

.net

wpf

xaml

I've made a style that makes 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:// ?

like image 805
Poma Avatar asked Feb 18 '12 12:02

Poma


1 Answers

try this

<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 text start with http//: then return true else return false. i hope this will help.

like image 119
yo chauhan Avatar answered Oct 24 '22 17:10

yo chauhan