1

我创建了一个简单的滚动查看器(pnlDayScroller),并希望有一个单独的水平滚动条(关联滚动条)来进行水平滚动。所有使用以下代码的作品都接受我需要绑定相关滚动条的可见性。

我不能简单地将其绑定到滚动查看器的水平模板部分的可见性属性,因为我已将其设置为始终隐藏。我能想到的唯一方法是将关联滚动条的可见性绑定到一个函数,这样

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

这可能吗?如果可以,我该怎么做?

    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

跟进(感谢 JustABill):

我已将此代码添加到上面的 pnlDayScroller 子中(我发现 scrollableWidth 是 scrollviewer 的属性而不是滚动条,但最大属性给出了我可以使用的结果)

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

我已经创建了这个类

 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

问题就解决了。

4

1 回答 1

1

你需要一个ValueConverter。绑定到 scrollableWidth 属性,并将您的 ValueConverter 添加到绑定的 Converter 属性。该示例使用 C# 编写,但概念非常简单,如果您看的话,我相信周围有 VB.Net 示例。

您需要做的简短形式是:

  1. 创建一个实现 IValueConverter 的新类(我认为它在 System.ComponentModel 中)。
  2. 使用您的第一个代码块填充 Convert 方法,除了使用“value”参数而不是 scrollableWidth 并返回可见性。
  3. 为您的本地类添加适当的 xmlns。
  4. 将新 ValueConverter 的 StaticResource 添加到 Window/UserControl/whatever 中。
  5. 使用此 ValueConverter 将 Visibility 属性绑定到 scrollableWidth 属性。
于 2010-06-07T15:02:47.540 回答