2

個人的な学習課題として、基本的なディップ スイッチ ユーザー コントロールを開発しています。もともと、ユーザー コントロールでいくつかのカスタム カラー プロパティを宣言できる場所に設定し、それらをコントロール内の要素で使用していました。

しかし、最近、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

デフォルトのグレーとライトグレーは VS2008 デザイナーとコンパイル済みアプリに表示されますが、ウィンドウで次のような操作を行うと:

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

このインスタンスに指定した色は使用されません。すべてがエラーなしでコンパイルされますが、私のコントロールはまだデフォルトの色で表示されています。

ToggleButton に項目をネストしたため、新しい階層が機能していると思います。

どんな助けでも大歓迎です。ありがとうございました。

4

2 に答える 2

2

色プロパティのゲッターで、ブラシに変換する必要があります

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

おそらく自動変換するだけなので違いはないかもしれませんが、試してみてください.

于 2008-12-12T15:52:05.667 に答える
0

子要素によって使用されるカスタムUserControlプロパティからの回答を参照してください。同じ概念をToggleButtonに適用できます。UserControl.TemplateオーバーライドのみのコンテンツなしでUserControlを作成し、TemplateBindingを使用して小道具を設定します

于 2008-12-12T21:34:05.323 に答える