views:

188

answers:

1

I've a created a simple scrollviewer (pnlDayScroller) and want to have a separate horizontal scrollbar (associated scroller) to do the horizontal scrolling. All works with the below code accept I need to bind the visibility of the associated scroller.

I can't simply bind this to the visibility property of the horizontal template part of the scroll viewer as I've set this to be always hidden. The only way I can think to do this is to bind the visibility of the associated scroller to a function such that

If associatedScroller.scrollableWidth > 0 then 
    associatedScroller.visibility = visibility.visible
else
    associatedScroller.visibility = visibility.collapsed
end if

Is this possible to do and if so how do I do it?

    Private Sub pnlDayScroller_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles pnlDayScroller.Loaded

        Dim binViewport, binMax, binMin, binSChange, binLChange As Binding


        Dim horizontalScrollBar As Primitives.ScrollBar = CType(pnlDayScroller.Template.FindName("PART_HorizontalScrollBar", pnlDayScroller), Primitives.ScrollBar)

        binViewport = New Binding("ViewportSize")
        binViewport.Mode = BindingMode.OneWay
        binViewport.Source = horizontalScrollBar
        associatedScroller.SetBinding(Primitives.ScrollBar.ViewportSizeProperty, binViewport)

        binMax = New Binding("Maximum")
        binMax.Mode = BindingMode.OneWay
        binMax.Source = horizontalScrollBar
        associatedScroller.SetBinding(Primitives.ScrollBar.MaximumProperty, binMax)

        binMin = New Binding("Minimum")
        binMin.Mode = BindingMode.OneWay
        binMin.Source = horizontalScrollBar
        associatedScroller.SetBinding(Primitives.ScrollBar.MinimumProperty, binMin)

        binSChange = New Binding("SmallChange")
        binSChange.Mode = BindingMode.OneWay
        binSChange.Source = horizontalScrollBar
        associatedScroller.SetBinding(Primitives.ScrollBar.SmallChangeProperty, binSChange)

        binLChange = New Binding("LargeChange")
        binLChange.Mode = BindingMode.OneWay
        binLChange.Source = horizontalScrollBar
        associatedScroller.SetBinding(Primitives.ScrollBar.LargeChangeProperty, binLChange)
End Sub

  Private Sub associatedScroller_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Double)) Handles associatedScroller.ValueChanged
        pnlDayScroller.ScrollToHorizontalOffset(e.NewValue)
end sub

FOLLOW UP (thanks to JustABill) :

I've add this code into the pnlDayScroller sub above (I've discovered scrollableWidth is a property of scrollviewer not scrollbar, but the maximum property gives a result I can use instead)

binVisibility = New Binding("Maximum")
    binVisibility.Mode = BindingMode.OneWay
    binVisibility.Source = horizontalScrollBar
    binVisibility.Converter = New ScrollableConverter
    associatedScroller.SetBinding(Primitives.ScrollBar.VisibilityProperty, binVisibility)

and I've created this class

 Public Class ScrollableConverter
        Implements IValueConverter

            Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object,
            ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert

            Dim dblMaximum As Double

            If targetType IsNot GetType(Visibility) Then
                Throw New InvalidOperationException("The target must be a visibility")
            Else


                dblMaximum = CType(value, Double)
                Debug.WriteLine("Value of double is " & dblMaximum)

                If dblMaximum > 0 Then
                    Return Visibility.Visible
                Else
                    Return Visibility.Collapsed
                End If
            End If

        End Function

        Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object,
            ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack

            Throw New NotSupportedException()
        End Function

End Class

And the problem is resolved.

A: 

You need a ValueConverter. Bind to the scrollableWidth property, and add your ValueConverter to the binding's Converter property. That example's in C#, but the concept's pretty simple, and I'm sure there's VB.Net examples around if you look.

The short form of what you need to do is:

  1. Create a new class that implements IValueConverter (I think it's in System.ComponentModel).
  2. Fill in the Convert method with your first code block, except use the "value" parameter instead of scrollableWidth and return the visibility.
  3. Add an appropriate xmlns for your local classes.
  4. Add a StaticResource of your new ValueConverter to your Window/UserControl/whatever.
  5. Bind the Visibility property to the scrollableWidth property using this ValueConverter.
JustABill