ToggleButton UserControl中的WPF模板绑定

时间:2008-12-12 15:00:24

标签: wpf user-controls dependency-properties

我正在开发一种基本的dip-switch用户控件作为个人学习练习。最初我设置了你可以在用户控件上声明一些自定义颜色属性的地方,它们将用在控件内的元素上。

然而,我最近发现了ToggleButtons,并重建了我的控件以利用它们。从那时起,我的自定义颜色属性(SwitchColor和SwitchBkgndColor)不再正常工作。它们总是使用默认颜色渲染,而不是我将其放置在窗口中时指定的颜色。这是一些代码:

    <UserControl x:Class="DipSwitchToggleBtn"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:app="clr-namespace:SwitchesApp"
        Width="20" Height="40">
        <ToggleButton Name="ToggleBtn" IsThreeState="False">
            <ToggleButton.Template>
                <ControlTemplate>

                    <Canvas Name="SwitchBkgnd"
                            Background="{TemplateBinding app:DipSwitchToggleBtn.SwitchBkgndColor}"
                            >

                        <Rectangle Name="SwitchBlock"
                                   Fill="{TemplateBinding app:DipSwitchToggleBtn.SwitchColor}"
                                   Width="16" Height="16"
                                   Canvas.Top="22"
                                   Canvas.Left="2"
                                   />

                    </Canvas>

                    <ControlTemplate.Triggers>

                    <Trigger Property="ToggleButton.IsChecked" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="SwitchBlock"
                                                     Duration="00:00:00.05"
                                                     Storyboard.TargetProperty="(Canvas.Top)" To="2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="SwitchBlock"
                                                     Duration="00:00:00.05"
                                                     Storyboard.TargetProperty="(Canvas.Top)" To="22" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>

                </ControlTemplate.Triggers>

                </ControlTemplate>
            </ToggleButton.Template>
        </ToggleButton>
    </UserControl>

......以及背后的代码:

Partial Public Class DipSwitchToggleBtn

    Public Property State() As Boolean
        Get
            Return Me.ToggleBtn.IsChecked
        End Get
        Set(ByVal value As Boolean)
            Me.ToggleBtn.IsChecked = value
        End Set
    End Property

    Public Sub Toggle()
        Me.State = Not Me.State
    End Sub

#Region " Visual Properties "

    Public Shared ReadOnly SwitchColorProperty As DependencyProperty = _
                           DependencyProperty.Register("SwitchColor", _
                           GetType(Brush), GetType(DipSwitchToggleBtn), _
                           New FrameworkPropertyMetadata(Brushes.LightGray))

    Public Property SwitchColor() As Brush
        Get
            Return GetValue(SwitchColorProperty)
        End Get

        Set(ByVal value As Brush)
            SetValue(SwitchColorProperty, value)
        End Set
    End Property


    Public Shared ReadOnly SwitchBkgndColorProperty As DependencyProperty = _
                           DependencyProperty.Register("SwitchBkgndColor", _
                           GetType(Brush), GetType(DipSwitchToggleBtn), _
                           New FrameworkPropertyMetadata(Brushes.Gray))

    Public Property SwitchBkgndColor() As Brush
        Get
            Return GetValue(SwitchBkgndColorProperty)
        End Get

        Set(ByVal value As Brush)
            SetValue(SwitchBkgndColorProperty, value)
        End Set
    End Property


#End Region

End Class

默认的Gray和LightGray出现在VS2008设计器和已编译的应用程序中,但是当我在窗口中执行类似的操作时:

<app:DipSwitchToggleBtn x:Name="DipSwitchTest" SwitchColor="#0000FF" SwitchBkgndColor="#000000" />

我为此实例指定的颜色不会被使用。一切都编译没有错误,但我的控件仍然以默认颜色显示。

我相信自从我在ToggleButton中嵌套我的项目后,有一些新的层次结构。

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:2)

在颜色属性的getter中,您需要转换为画笔

Public Property SwitchBkgndColor() As Brush
    Get
        Return CType(GetValue(SwitchBkgndColorProperty), Brush)
    End Get

    Set(ByVal value As Brush)
        SetValue(SwitchBkgndColorProperty, value)
    End Set
End Property

它可能没什么区别,因为它可能只是自动转换但尝试一下。

答案 1 :(得分:0)

请参阅Custom UserControl Property used by child element的答案。相同的概念可以应用于您的ToggleButton。创建没有内容的UserControl只是一个UserControl.Template覆盖并使用TemplateBinding来设置你的道具

豫ICP备18024241号-1