2010-02-15 126 views
1

嗯,我叫對話框苗條下來的問題 這裏自定義控制我的vb.net代碼:WPF CustomControl命令和數據綁定

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它的作品,爲什麼我不能綁定到一個命令定義和設置codebehind(或控制代碼)?

回答

1

什麼是您的DataContext?你需要設置:

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

這似乎並沒有解決問題:(,似乎無法找到的命令,我想念使用只讀DP – Peter 2010-02-15 11:29:49

+1

看在調試輸出窗口綁定?錯誤 – 2010-02-15 12:08:02

+0

似乎它尋找父容器上的命令在我的情況窗口...有沒有辦法告訴它自我看?System.Windows.Data錯誤:39:BindingExpression路徑錯誤:'YesCommand' property'not'on'object'''Window1'(Name ='')'。BindingExpression:Path = YesCommand; DataItem ='Window1'(Name ='');目標元素是'Button'(Name ='');目標屬性是'命令'(類型'ICommand') – Peter 2010-02-15 12:16:07