1

さて、私はそれを問題にスリム化するためにDialogと呼ばれるカスタムコントロールを持っていますこれが私のvb.netコードです:

Public Class Dialog
    Inherits System.Windows.Controls.Control

#Region "DependencyProperties"

    Public Shared ReadOnly OkCommandProperty As DependencyProperty = _
                           DependencyProperty.Register("OkCommand", _
                           GetType(ICommand), GetType(Dialog), _
                           New FrameworkPropertyMetadata(Nothing))

    Private Shared ReadOnly YesCommandPropertyKey As DependencyPropertyKey = _
                            DependencyProperty.RegisterReadOnly("YesCommand", _
                            GetType(ICommand), GetType(Dialog), _
                            New FrameworkPropertyMetadata(Nothing))

    Public Shared ReadOnly YesCommandProperty As DependencyProperty = _
                           YesCommandPropertyKey.DependencyProperty

#End Region

    Public ReadOnly Property YesCommand() As ICommand
        Get
            Return CType(GetValue(ConfirmationDialog.YesCommandProperty), ICommand)
        End Get
    End Property



    Public Sub New()
        MyBase.New()
        SetValue(ConfirmationDialog.YesCommandPropertyKey, New RelayCommand(AddressOf Yes))
    End Sub


#Region "Commands"
    Public Property OkCommand() As ICommand
        Get
            Return CType(GetValue(OkCommandProperty), ICommand)
        End Get
        Set(ByVal value As ICommand)
            SetValue(OkCommandProperty, value)
        End Set
    End Property
#End Region

#Region "Functions"
    Sub Ok()
        Dim command As ICommand = OkCommand
        If (command Is Nothing AndAlso command.CanExecute(Nothing)) Then
            command.Execute(Nothing)
        End If
    End Sub

    Sub Yes(ByVal parameter As Object)
        Ok()
        Me.Visibility = Windows.Visibility.Collapsed
    End Sub
#End Region

    Shared Sub New()
        'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
        'This style is defined in themes\generic.xaml
        DefaultStyleKeyProperty.OverrideMetadata(GetType(Dialog), New FrameworkPropertyMetadata(GetType(Dialog)))
    End Sub
End Class

これが私のxamlです:

<Style TargetType="{x:Type local:Dialog}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Dialog}">
               <Button Content="Yes" Command="{Binding YesCommand}"/> 
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

しかし、YesCommandは機能していないようですが、OkCommandを配置すると機能するので、コードビハインド(または制御コード)で定義および設定されているコマンドにバインドできないのはなぜですか?

4

1 に答える 1

1

あなたの DataContext は何ですか? 設定する必要がありますか:

Public Sub New()
    MyBase.New()
    SetValue(ConfirmationDialog.YesCommandPropertyKey, New RelayCommand(AddressOf Yes))
    DataContext = Me
End Sub
于 2010-02-15T11:06:13.223 に答える