2010-02-15 125 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