tags:

views:

1796

answers:

3

I have the following style:

<Style x:Key="ActionLabelStyle" TargetType="{x:Type Label}">
    <Setter Property="Margin" Value="10,3" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
    <Setter Property="FontFamily" Value="Calibri" />
    <Style.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsMouseOver" Value="True" />
                <Condition Property="IsEnabled" Value="True" />
            </MultiTrigger.Conditions>
            <Setter Property="Background" Value="Red" />
            <Setter Property="TextBlock.TextDecorations" Value="Underline" />
        </MultiTrigger>
    </Style.Triggers>
</Style>

So basically, I want to have a label which is underlined when it is enabled and the mouse cursor is over it. The part of this style which is not working is the <Setter Property="TextBlock.TextDecorations" Value="Underline" />. Now, what am I doing wrong here? Thanks for all the help.

+2  A: 

I think the issue is that TextBlock.TextDecorations is not defined on Label.

You can use this approach if you're happy to use a TextBlock rather than a Label.

Drew Noakes
Thank you for your answer.
Boris
+7  A: 

This is actually much more difficult than it appears. In WPF, a Label is not a TextBlock. It derives from ContentControl and can therefore host other, non-text controls in its Content collection.

However, you can specify a string as the content as in the example below. Internally, a TextBlock will be constructed to host the text for you.

<Label Content="Test!"/>

This internally translates to:

    <Label>
        <Label.Content>
            <TextBlock>
                Test!
            </TextBlock>
        </Label.Content>
    </Label>

The simple solution to this would be for the TextDecorations property of a TextBlock to be an attached property. For example, FontSize is designed this way, so the following works:

    <Label TextBlock.FontSize="24">
        <Label.Content>
            <TextBlock>
                Test!
            </TextBlock>
        </Label.Content>
    </Label>

The TextBlock.FontSize attached property can be applied anywhere in the visual tree and will override the default value for that property on any TextBlock descendant in the tree. However, the TextDecorations property is not designed this way.

This leaves you with at least a few options.

  1. Use color, border, cursor, etc., instead of underlined text because this is 100% easier to implement.
  2. Change the way you are doing this to apply the Style to the TextBlock instead.
  3. Go to the trouble to create your own attached property and the control template to respect it.
  4. Do something like the following to nest the style for TextBlocks that appear as children of your style:

FYI, this is the ugliest thing I've done in WPF so far, but it works!

    <Style x:Key="ActionLabelStyle" TargetType="{x:Type Label}">
        <Setter Property="Margin" Value="10,3" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="TextBlock.TextWrapping" Value="Wrap" />
        <Setter Property="FontFamily" Value="Calibri" />
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver" Value="True" />
                    <Condition Property="IsEnabled" Value="True" />
                </MultiTrigger.Conditions>
                <Setter Property="Background" Value="Red" />
            </MultiTrigger>
        </Style.Triggers>
        <Style.Resources>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Label}, Path=IsMouseOver}" Value="True" />
                            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}" Value="True" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="TextDecorations" Value="Underline"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>

This works because it is overriding the default style of any TextBlock beneath a Label of this style. It then uses a MultiDataTrigger to allow relative binding back up to the Label to check if its IsMouseOver property is True. Yuck.

Edit:

Note that this only works if you explicitly create the TextBlock. I was incorrect when I posted this because I had already dirtied up my test Label. Boo. Thanks, Anvaka, for pointing this out.

    <Label Style="{StaticResource ActionLabelStyle}">
        <TextBlock>Test!</TextBlock>
    </Label>

This works, but if you have to go to this trouble, you're just working too hard. Either someone will post something more clever, or as you said, my option 1 is looking pretty good right now.

Jerry Bullard
Jerry, you really did a lot of work here. Thank you for your efforts. I think I'll just go with the first option that you have recommended. The style you have provided does the trick, but it is also "yuck" as you stated - yuck indeed. I'm marking your answer as correct. Again, thanks for the reply!
Boris
Thanks, Boris. I did do more work than usual for an SO answer, but similar problems to this one have been bugging me for some time now, so the research I did was pretty helpful for my own edification, too. :)
Jerry Bullard
+1 -- nice answer Jerry. I learned something, even if I wish I hadn't :)
Drew Noakes
Good answer :). You don't need to have IsEnabled property in the trigger. If it's disabled it doesn't receive MouseOver event. Also it will not work if you set content right in the label: <Label Style="{StaticResource ActionLabelStyle}" Content="Hey"/>, so be careful.
Anvaka
Thanks, Anvaka. I updated the answer to include your insight. Shame on me for not testing this properly. I had already updated my Label with the explicit TextBlock beneath it.
Jerry Bullard
+1  A: 

Further to Jerry's answer, in order to avoid having to add the TextBlock into the template each time you can let the style do this too by adding the Setter property into the style:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Label}">
            <TextBlock>
                <ContentPresenter />
            </TextBlock>
        </ControlTemplate>
    </Setter.Value>
</Setter>

Then your Label is back to:

<Label Content="Test!" Style="{StaticResource ActionLabelStyle}" />

Thanks Jerry!

Andrew.

Reddog
this looks great - but ContentPresenter doesn't seem to be valid in that location
Simon_Weaver