tags:

views:

25

answers:

1

Well i have a custom control called Dialog to slim it down to the problem Here is my vb.net code:

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

and here is my 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>

But YesCommand does not seem to work, but if i put OkCommand it works so why can't i bind to a command thats defined and set in codebehind (or the control code)?

+1  A: 

What is your DataContext? Do you need to set it:

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

HTH,
Kent

Kent Boogaart
this does not seem to solve the problem :(, it seems it can't find the command, am i miss using readonly dp?
Petoj
look in the debug output window for binding errors
Kent Boogaart
It seems its looking for the command on the parent container a window in my case... is there no way to tell it to look on it self? System.Windows.Data Error: 39 : BindingExpression path error: 'YesCommand' property not found on 'object' ''Window1' (Name='')'. BindingExpression:Path=YesCommand; DataItem='Window1' (Name=''); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')
Petoj
Got it working with Command="{Binding Path=YesCommand, RelativeSource={RelativeSource AncestorType={x:Type local:Dialog}}}
Petoj