0

I have a WPF TextBlock with a border applied. See below:

<Border BorderBrush="Transparent" 
        BorderThickness="0"
        Visibility="Visible">
    <Border.Style>
        <Style>
            <Setter Property="Border.BorderBrush" Value="Transparent" />
            <Setter Property="Border.BorderThickness" Value="0" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=MyScrollViewer, Path=VerticalScrollBarVisibility}" Value="Visible">
                    <Setter Property="Border.BorderBrush" Value="NavajoWhite" />
                    <Setter Property="Border.BorderThickness" Value="2" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <ScrollViewer x:Name="MyScrollViewer"
                  HorizontalScrollBarVisibility="Disabled"
                  VerticalScrollBarVisibility="Auto">
        <TextBlock Grid.Column="1"
                   VerticalAlignment="Stretch"
                   HorizontalAlignment="Stretch"
                   Height="auto"
                   Margin="5"
                   Text="{Binding Path=Text}" 
                   TextWrapping="Wrap"
                   Foreground="{Binding Path=ForegroundColor}">
        </TextBlock>
    </ScrollViewer>
</Border>

I am trying to show the border ONLY when the vertical scroll bar is visible (VerticalScrollBarVisibility = "Visible") so I have applied an style for the border with a trigger that in theory should do the trick but it is not working. Instead of using Visibility property for border I am playing with the BorderBrush and BorderThickness properties. Border Visibility is always true. When I want to hide it, it set BorderBrush to transparent and BorderThickness to a value greater than 0. If I want to make border visible, then I set a color for BorderBrush different from transparent and set BorderThickness to 0. For some reason it is not working, seems trigger is not fired. By default the border should be not visible because initially no vertical scrollbar is shown.

1 Answer 1

1

When you set properties on an element, you cannot override them with a style. So first thing is to remove them from Border (which I think you intended because I see it in the style setters).

To fix your trigger problem, you just need the right property. VerticalScrollBarVisibility will always be Auto because you set it. What you want is ComputedVerticalScrollBarVisibility.

I also set the Style TargetType so you get less verbosity.

<Border>
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=MyScrollViewer, Path=ComputedVerticalScrollBarVisibility}" Value="Visible">
                    <Setter Property="BorderBrush" Value="NavajoWhite" />
                    <Setter Property="BorderThickness" Value="2" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <ScrollViewer x:Name="MyScrollViewer"
                  HorizontalScrollBarVisibility="Disabled"
                  VerticalScrollBarVisibility="Auto">
        <TextBlock Grid.Column="1"
                   VerticalAlignment="Stretch"
                   HorizontalAlignment="Stretch"
                   Height="auto"
                   Margin="5"
                   Text="{Binding Path=Text}" 
                   TextWrapping="Wrap"
                   Foreground="{Binding Path=ForegroundColor}">
        </TextBlock>
    </ScrollViewer>
</Border>
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, VerticalScrollBarVisibility will always be Auto. I was taking into account the wrong property. Thanks, it worked! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.