WPF ControlTemplate和附加属性 - 绑定

时间:2016-08-20 14:04:33

标签: c# wpf controltemplate attached-properties

美好的一天,

我正在尝试在控件模板的MultyBinding中使用附加属性。我似乎无法从Expander / Button获取值到MultyBinding中,以便它可以在后面的一些代码中使用。

也许我在这里(可能)已经过度复杂了,但所有这些都与需要设置自定义属性的自定义动画有关。

附加属性是APMinWidth和APMaxWidth。它们可以设置在按钮的扩展器上 - 并不重要。控件模板处理动画和其他方面,但需要通过MultyBinding将附加属性的值传递给类。该类是ExtraCodeBehind:CalculateWidth。

以下所有相关代码。我错过了什么?我错了吗?

P.S。因为这是一个新项目,所以代码可以很容易地完全重构。

附属物:

Namespace WpfApplication1.View.ViewStyles.ExtraCodeBehind
    Public Class animationsAP
        Inherits DependencyObject

        Public Shared ReadOnly APMinWidthProperty As DependencyProperty = DependencyProperty.RegisterAttached("APMinWidth", GetType(System.Double), GetType(animationsAP), New FrameworkPropertyMetadata(CDbl(0), AddressOf APMinWidthPropertyChanged))
        Public Shared ReadOnly APMaxWidthProperty As DependencyProperty = DependencyProperty.RegisterAttached("APMaxWidth", GetType(System.Double), GetType(animationsAP), New FrameworkPropertyMetadata(CDbl(0), AddressOf APMaxWidthPropertyChanged))

        Public Shared Sub SetAPMinWidthProperty(ByVal element As UIElement, ByVal value As System.Double)
            element.SetValue(APMinWidthProperty, value)
        End Sub
        Public Shared Function GetAPMinWidthProperty(ByVal element As UIElement) As System.Double
            Return CType(element.GetValue(APMinWidthProperty), Boolean)
        End Function

        Private Shared Sub APMinWidthPropertyChanged(ByVal d As System.Windows.DependencyObject, ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
            'Dim scrollViewer = TryCast(d, ScrollViewer)

            'If scrollViewer IsNot Nothing AndAlso CBool(e.NewValue) Then
            '    scrollViewer.ScrollToBottom()
            'End If
        End Sub

        Public Shared Sub SetAPMaxWidthProperty(ByVal element As UIElement, ByVal value As System.Double)
            element.SetValue(APMaxWidthProperty, value)
        End Sub
        Public Shared Function GetAPMaxWidthProperty(ByVal element As UIElement) As System.Double
            Return CType(element.GetValue(APMaxWidthProperty), Boolean)
        End Function

        Private Shared Sub APMaxWidthPropertyChanged(ByVal d As System.Windows.DependencyObject, ByVal e As System.Windows.DependencyPropertyChangedEventArgs)
            'Dim scrollViewer = TryCast(d, ScrollViewer)

            'If scrollViewer IsNot Nothing AndAlso CBool(e.NewValue) Then
            '    scrollViewer.ScrollToBottom()
            'End If
        End Sub
    End Class
End Namespace

控制模板:

<ResourceDictionary [...]
    xmlns:View="clr-namespace:WpfApplication1.View"                                         
    xmlns:animationAP="clr-namespace:WpfApplication1.WpfApplication1.View.ViewStyles.ExtraCodeBehind"
    xmlns:ExtraCodeBehind="clr-namespace:WpfApplication1.WpfApplication1.View.ViewStyles.ExtraCodeBehind">

    <ExtraCodeBehind:CalculateWidth x:Key="CalculateWidth" />
    <ControlTemplate x:Key="RevealExpanderTemp" TargetType="{x:Type Expander}">
        <DockPanel>
            <ScrollViewer x:Name="ExpanderContentScrollView" [...]>
                <ScrollViewer.Tag>                    
                    <sys:Double>0.0</sys:Double>
                </ScrollViewer.Tag>                
                <ScrollViewer.Width>
                    <MultiBinding Converter="{StaticResource CalculateWidth}">
                        <Binding Path="ActualWidth" ElementName="ExpanderContent" />
                        <Binding Path="Tag" RelativeSource="{RelativeSource Self}" />
                        <Binding Path="animationAP:animationsAP.APMinWidthProperty" ElementName="Expander" />
                        <Binding Path="animationAP:animationsAP.APMaxWidthProperty" ElementName="Expander" />
                    </MultiBinding>
                </ScrollViewer.Width>
                <ContentPresenter x:Name="ExpanderContent" ContentSource="Content" />
            </ScrollViewer>
        </DockPanel>
        <ControlTemplate.Triggers>
            [... some triggers here ...]
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>

实施代码:

<Expander Template="{StaticResource RevealExpanderTemp}" IsExpanded="True" animationAP:animationsAP.APMinWidthProperty="20.0" animationAP:animationsAP.APMaxWidthProperty="70.0" >
    <Button  Width="70px"  MaxWidth="70px" MinWidth="20px"  >
    [... rest of definition here ...]
    </Button>
</Expander>

0 个答案:

没有答案