Wpf 绑定到函数

发布于 2024-09-04 22:59:32 字数 3830 浏览 3 评论 0原文

我创建了一个简单的滚动查看器(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

问题就解决了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

廻憶裏菂餘溫 2024-09-11 22:59:32

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

您需要做的简短形式是:

  1. 创建一个实现 IValueConverter 的新类(我认为它位于 System.ComponentModel 中)。
  2. 使用您的第一个代码块填充 Convert 方法,但使用“value”参数而不是scrollableWidth 并返回可见性。
  3. 为您的本地类添加适当的 xmlns。
  4. 将新 ValueConverter 的 StaticResource 添加到您的 Window/UserControl/其他内容。
  5. 使用此 ValueConverter 将 Visibility 属性绑定到scrollableWidth 属性。

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文