Question

How can I make multiple TextBlock texts underlined when mouse is over any of them?

<StackPanel Orientation="Horizontal">
     <TextBlock Style="{StaticResource UnderlineStyle}" Text="{Binding FirstValue}" />
     <TextBlock Style="{StaticResource UnderlineStyle}" Text=" - " />
     <TextBlock Style="{StaticResource UnderlineStyle}" Text="{Binding SecondValue}" />
</StackPanel>

<Style x:Key="UnderlineStyle" TargetType="TextBlock">
     <Style.Triggers>
          <Trigger Property="IsMouseOver" Value="True">
               <Setter Property="TextDecorations" Value="Underline" />
           </Trigger>
     </Style.Triggers>
</Style>

The above code underlines any of the TextBlocks independently of each other but I need all of them all to be underlined when any of them is mouse-overed.

Was it helpful?

Solution

You should use a DataTrigger with MultiBinding -

<MultiBinding Converter="{StaticResource myConverter}">   
  <Binding ElementName="textBlock1" Path="IsMouseOver" />
  <Binding ElementName="textBlock2" Path="IsMouseOver" />
  <Binding ElementName="textBlock3" Path="IsMouseOver" />
</MultiBinding>

and in the converter if one of them is true, return true.

Another option (the first one is better) is to have 3 DataTriggers for IsMouseOver any of the textblocks (give them names, and use ElementName in the Binding).

<DataTrigger Binding="{Binding ElementName=textBlock1, Path=IsMouseOver}" Value="True" >
    <Setter Property="TextDecorations" Value="Underline" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=textBlock2, Path=IsMouseOver}" Value="True" >
    <Setter Property="TextDecorations" Value="Underline" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=textBlock3, Path=IsMouseOver}" Value="True" >
    <Setter Property="TextDecorations" Value="Underline" />
</DataTrigger>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top