Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding a Label's Visibility to a ScrollViewer's ComputedVerticalScrollBarVisibility

I have a ScrollViewer, it scrolls vertically only, and shows the vertical scroll bar if it needs to only:

<ScrollViewer x:Name="sv" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto">

I also have a Label, which I only want to show if the ScrollViewer's vertical scroll bar is showing:

<Label Background="DarkBlue" Height="60" Width="70">
            <Label.Style>
                <Style TargetType="Label">
                    <Setter Property="Visibility" Value="Visible"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ComputedVerticalScrollBarVisibility.Visibility, ElementName=sv}" Value="Hidden">
                            <Setter Property="Visibility" Value="Hidden"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
</Label>

This does not seem to work. I have searched for a solution, which normally I find quite quickly hence this being my first post. Any suggestions on how to get this working? I would prefer a xaml only solution but could be convinced to use converters and what-not.

like image 928
CornishCoder Avatar asked Mar 19 '23 22:03

CornishCoder


2 Answers

First ComputedVerticalScrollBarVisibility property is of type Visibility. So, you have to bind with ComputedVerticalScrollBarVisibility property only and not with ComputedVerticalScrollBarVisibility.Visibility.

Second, Value will be Collapsed and not Hidden


Update trigger to this:

<DataTrigger Binding="{Binding ComputedVerticalScrollBarVisibility,
                               ElementName=sv}" Value="Collapsed">
    <Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
like image 138
Rohit Vats Avatar answered Mar 31 '23 12:03

Rohit Vats


The simplest way is probably to bind your label's visibility directly to the scrollbar's computed visibility:

<Label Background="DarkBlue" Height="60" Width="70"
       Visibility="{Binding ComputedVerticalScrollBarVisibility, ElementName=sv}" />

This way the label is only visible when the scrollbar is visible. Your example uses Hidden and not Collapsed. If you want to only hide the label and not collapse it you can use a converter for that.

like image 20
Frode Evensen Avatar answered Mar 31 '23 10:03

Frode Evensen