views:

6370

answers:

2

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.

+2  A: 

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.

Bryan Anderson
Sorry, just tried it, but no change. The DependencyProperty code above worked perfectly with different XAML in an earlier attempt of mine.
B2Ben
Hmm, I don't know then. The rest of the code looks fine to me but I'm still pretty new to this stuff.
Bryan Anderson
A: 

See the answer from http://stackoverflow.com/questions/358443/custom-usercontrol-property-used-by-child-element#360700. The same concept can be applied for your ToggleButton. Create UserControl with no content just a UserControl.Template override and use TemplateBinding to set your props

bendewey
I tried something like the example from my other post you referenced. Unfortunately My animation triggers could not access my custom "State" property for some reason, which is why I tried the ToggleButton-based control, where I could access IsChecked for my animations. Maybe I'll post a new question
B2Ben