title: Wpf binding to a function
body: <p>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.  </p>
  
  <p>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</p>
  
  <pre><code>If associatedScroller.scrollableWidth &gt; 0 then 
      associatedScroller.visibility = visibility.visible
  else
      associatedScroller.visibility = visibility.collapsed
  end if
  </code></pre>
  
  <p>Is this possible to do and if so how do I do it?</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>FOLLOW UP (thanks to JustABill) :</p>
  
  <p>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)</p>
  
  <pre><code>binVisibility = New Binding("Maximum")
      binVisibility.Mode = BindingMode.OneWay
      binVisibility.Source = horizontalScrollBar
      binVisibility.Converter = New ScrollableConverter
      associatedScroller.SetBinding(Primitives.ScrollBar.VisibilityProperty, binVisibility)
  </code></pre>
  
  <p>and I've created this class</p>
  
  <pre><code> 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 " &amp; dblMaximum)
  
                  If dblMaximum &gt; 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
  </code></pre>
  
  <p>And the problem is resolved.</p>