2010-02-15 9 views
1

Eh bien j'ai un contrôle personnalisé appelé Dialog à perdre du poids vers le bas au problème Voici mon code vb.net:WPF CustomControl Commandes et databinding

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 

et voici mon 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> 

Mais YesCommand ne semble pas fonctionner, mais si je mets OkCommand cela fonctionne donc pourquoi ne puis-je pas lier à une commande qui est définie et définie dans codebehind (ou le code de contrôle)?

Répondre

1

Quel est votre DataContext? Avez-vous besoin de le mettre:

Public Sub New() 
    MyBase.New() 
    SetValue(ConfirmationDialog.YesCommandPropertyKey, New RelayCommand(AddressOf Yes)) 
    DataContext = Me 
End Sub 
+0

cela ne semble pas résoudre le problème :(, il semble qu'il ne trouve pas la commande, je suis me manque à l'aide readonly dp – Peter

+1

regard dans la fenêtre de sortie de débogage pour la liaison? erreurs –

+0

Il semble qu'il cherche la commande sur le conteneur parent une fenêtre dans mon cas ... est-il impossible de lui dire de se regarder sur lui-même? System.Windows.Data Erreur: 39: BindingExpression erreur de chemin: 'YesCommand' propriété introuvable sur 'objet' '' Window1 '(Name =' ')'. BindingExpression: Path = YesCommande; DataItem = 'Window1' (Name = ''); l'élément cible est 'Button' (Name = ''); La propriété target est 'Command' (type 'ICommand') – Peter