WPF结合到功能 [英] Wpf binding to a function

查看:149
本文介绍了WPF结合到功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建了一个简单的ScrollViewer(pnlDayScroller),并希望有一个单独的水平滚动条(相关滚轮)做水平滚动。用低于code所有作品接受我需要到相关滚轮的知名度绑定。

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

跟进(感谢JustABill):

FOLLOW UP (thanks to JustABill) :

我已经添加此code到pnlDayScroller分以上(我发现scrollableWidth是ScrollViewer中的属性没有滚动条,但最高属性给出的结果我可以改用)

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)

和我创建了这个类

 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

和问题得到解决。

推荐答案

您需要的 ValueConverter 。绑定到scrollableWidth属性,你ValueConverter添加到绑定的转换器属性。这个例子是在C#中,但这个概念的pretty简单,我敢肯定有各地VB.Net的例子,如果你看一下。

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. 创建一个实现的IValueConverter一个新类(我认为这是在System.ComponentModel)。

  2. 在转换方法
  3. 填写首次code座,除了使用价值的参数,而不是scrollableWidth并返回知名度。

  4. 添加适当的xmlns为您的本地类。

  5. 添加静态资源新ValueConverter到您的窗口/用户控件/不管。

  6. 使用此ValueConverter Visibility属性绑定到scrollableWidth属性。

这篇关于WPF结合到功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆