2010-06-07 5 views
0

J'ai créé un scrollviewer simple (pnlDayScroller) et je veux avoir une barre de défilement horizontale séparée (scroller associé) pour faire le défilement horizontal. Tous les travaux avec le code ci-dessous acceptent que je doive lier la visibilité du scroller associé.Wpf liaison à une fonction

Je ne peux pas simplement lier cela à la propriété de visibilité de la partie du modèle horizontal de la visionneuse de défilement, car j'ai défini cela comme toujours caché. La seule façon que je peux penser à faire est de lier la visibilité de la barre de défilement associée à une fonction telle que

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

Est-ce possible de le faire et si oui, comment puis-je le faire?

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 

SUIVI (grâce à JustABill):

J'ai ajouter ce code dans le sous pnlDayScroller ci-dessus (je l'ai découvert scrollableWidth est une propriété de ScrollViewer pas scrollbar, mais la propriété maximale donne une résultat que je peux utiliser à la place)

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

et j'ai créé cette classe

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 

Et le problème est résolu.

+0

Après les commentaires de JustABill j'ai ajouté quelques followup ci-dessus – user360349

Répondre

0

Vous avez besoin d'un ValueConverter. Liez à la propriété scrollableWidth et ajoutez votre ValueConverter à la propriété Converter de la liaison. Cet exemple est en C#, mais le concept est assez simple, et je suis sûr qu'il y a des exemples de VB.Net si vous regardez.

La forme courte de ce que vous devez faire est:

  1. Créer une nouvelle classe qui implémente IValueConverter (je pense qu'il est en System.ComponentModel).
  2. Remplissez la méthode Convert avec votre premier bloc de code, sauf utilisez le paramètre "value" au lieu de scrollableWidth et renvoyez la visibilité. Ajoutez un xmlns approprié pour vos classes locales. Ajoutez une StaticResource de votre nouveau ValueConverter à votre fenêtre/UserControl/quoi que ce soit.
  3. Liez la propriété Visibility à la propriété scrollableWidth à l'aide de ce ValueConverter.
Questions connexes