代码之家  ›  专栏  ›  技术社区  ›  Ben Brandt

togglebutton用户控件中的wpf模板绑定

  •  2
  • Ben Brandt  · 技术社区  · 16 年前

    我正在开发一个基本的DIP开关用户控件作为个人学习练习。最初,我将它设置为可以在用户控件上声明一些自定义颜色属性的位置,这些属性将用于控件内的元素。

    然而,我最近发现了切换按钮,并重新构建了控件以利用它们。从那时起,我的自定义颜色属性(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
    

    默认的灰色和浅灰色显示在VS2008设计器和编译的应用程序中,但当我在窗口中执行类似操作时:

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

    我为此实例指定的颜色无法使用。所有内容都编译无误,但我的控件仍以默认颜色显示。

    我相信有一些新的层次在发挥作用,因为我嵌套我的项目在切换按钮。

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

    2 回复  |  直到 16 年前
        1
  •  2
  •   Bryan Anderson    16 年前

    在颜色属性的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
    

    它可能不会有什么不同,因为它可能只是自动转换,但给它一个尝试。

        2
  •  0
  •   Community CDub    8 年前

    从中看到答案 Custom UserControl Property used by child element 。同样的概念也适用于您的切换按钮。创建不包含任何内容的用户控件。模板重写并使用TemplateBinding设置道具