ToggleButton UserControl中的WPF模板绑定 [英] WPF Template Binding in ToggleButton UserControl

查看:88
本文介绍了ToggleButton UserControl中的WPF模板绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



但是,我最近发现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>

<画布名称= SwitchBkgnd
Background = {TemplateBinding app:DipSwitchToggleBtn.SwitchBkgndColor}
>

<矩形名称= SwitchBlock
Fill = {TemplateBinding应用:DipSwitchToggleBtn.SwitchColor}
Width = 16 Height = 16
Canvas.Top = 22
Canvas.Left = 2
/>

< /画布>

< 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>

...以及后面的代码:

 局部公共类DipSwitchToggleBtn 

Public Property State()as Boolean
Get
Return Me.ToggleBtn.IsChecked
End获取
Set(ByVal值作为布尔值)
Me.ToggleBtn.IsChecked =值
最终设置
最终属性

Public Sub Toggle()
Me.State =不是Me.State
结束子

#Region视觉属性

公开共享的只读SwitchColorProperty作为DependencyProperty = _
DependencyProperty.Register( SwitchColor,_
GetType(Brush),GetType(DipSwitchToggleBtn),_
New FrameworkPropertyMetadata(Brushes.LightGray))

公共属性SwitchColor()画笔
获取
返回GetValue(SwitchColorProperty)
结束Get

Set(ByVal值作为画笔)
SetValue(SwitchColorProperty,值)
结束集合
结束属性


公开共享的只读SwitchBkgndColorProperty作为DependencyProperty = _
DependencyProperty.Register( SwitchBkgndColor,_
GetType(Brush),GetType(DipSwitchToggleBtn),_
New FrameworkPropertyMetadata(Brushes.Gray))

公共属性SwitchBkgndColor ()作为画笔
获取
返回GetValue(SwitchBkgndColorProperty)
结束Get

Set(ByVal值作为画笔)
SetValue(SwitchBkgndColorProperty,value)
最终集合
最终财产


#End Region

最终阶级

默认的Gray和LightGray显示在VS2008设计器和编译的应用程序中,但是当我在窗口中执行以下操作时:

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

我为此实例指定的颜色未使用。一切都可以正确编译,但我的控件仍显示为默认颜色。



我相信,由于我将项目嵌套在ToggleButton中,因此正在发挥一些新的层次结构。 / p>

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

解决方案

在获取颜色属性的方法中,您需要转换为画笔

 公共属性SwitchBkgndColor()作为画笔
获取
返回CType(GetValue(SwitchBkgndColorProperty),画笔)
结束获取

Set(ByVal值作为画笔)
SetValue(SwitchBkgndColorProperty,值)
最终设置
最终属性

它可能没有什么不同,因为它可能只是自动转换,而是尝试一下。


I'm developing a basic dip-switch user control as a personal learning exercise. Originally I had it set up where you could declare some custom color properties on the user control, and they would be used on elements inside the control.

However, I recenly discovered ToggleButtons, and rebuilt my control to take advantage of them. Since then, my custom color properties (SwitchColor and SwitchBkgndColor) no longer work properly. They are always rendered with the default colors, not the colors I specified when I place them in my Window. Here's some code:

    <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>

...and the code behind:

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

The default Gray and LightGray show up in the VS2008 designer and the compiled app, but when I do something like this in my window:

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

The colors I specified for this instance do not get used. Everything compiles without error, but my control is still displayed with the default colors.

I believe there is some new hierarchy at play since I nested my items in the ToggleButton.

Any help would be appreciated. Thank you.

解决方案

In the getters of your color properties you need to convert to brushes

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

It might not make a difference as it probably just auto-converts but give it a try.

这篇关于ToggleButton UserControl中的WPF模板绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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