views:

9

answers:

1

Hi, I'm having problems attaching an uncheck command to a checkbox. Or more correct, I do not know how to code it. Here's my code for the check command, how should it look to get uncheck also working?

View:

<CheckBox commands:Checked.Command="{Binding CheckCommand}"
IsChecked="False"></CheckBox>

ViewModel:

Private _CheckCommand As DelegateCommand(Of Object)

CheckCommand = New DelegateCommand(Of Object)(AddressOf Checked)

Private Sub Checked(ByVal parameter As Object)

End Sub

Command:

Public Class ToggleCheckedCommandBehaviour
    Inherits CommandBehaviorBase(Of CheckBox)

    Public Sub New(ByVal checkableObject As CheckBox)
        MyBase.New(checkableObject)
        AddHandler checkableObject.Checked, AddressOf checkableObject_Checked
    End Sub

    Private Sub checkableObject_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        CommandParameter = TargetObject.Name
        ExecuteCommand()
    End Sub
End Class

Public NotInheritable Class Checked

    Private Sub New()
    End Sub

    Private Shared ReadOnly SelectedCommandBehaviorProperty As DependencyProperty = _
    DependencyProperty.RegisterAttached("SelectedCommandBehavior", _
                                        GetType(ToggleCheckedCommandBehaviour), _
                                        GetType(Checked), _
                                        Nothing)

    Private Shared ReadOnly CommandProperty As DependencyProperty = _
    DependencyProperty.RegisterAttached("Command", _
                                        GetType(ICommand), _
                                        GetType(Checked), _
                                        New PropertyMetadata(AddressOf OnSetCommandCallback))


    Public Shared Sub SetCommand(ByVal CheckBox As CheckBox, ByVal command As ICommand)
        CheckBox.SetValue(CommandProperty, command)
    End Sub

    Public Shared Function GetCommand(ByVal CheckBox As CheckBox) As ICommand
        Return TryCast(CheckBox.GetValue(CommandProperty), ICommand)
    End Function

    Private Shared Sub OnSetCommandCallback(ByVal dependencyObject As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim CheckBox = TryCast(dependencyObject, CheckBox)
        If Not CheckBox Is Nothing Then
            Dim behavior = GetOrCreateBehavior(CheckBox)
            behavior.Command = TryCast(e.NewValue, ICommand)
        End If
    End Sub

    Private Shared Function GetOrCreateBehavior(ByVal CheckBox As CheckBox) As ToggleCheckedCommandBehaviour
        Dim behavior = TryCast(CheckBox.GetValue(SelectedCommandBehaviorProperty), ToggleCheckedCommandBehaviour)
        If behavior Is Nothing Then
            behavior = New ToggleCheckedCommandBehaviour(CheckBox)
            CheckBox.SetValue(SelectedCommandBehaviorProperty, behavior)
        End If
        Return behavior
    End Function

End Class
End Namespace

As mentioned the check command works fine, and the command and method connected to it gets fires, what do I need to do to get the uncheck also working? For info I'm using PRISM, CAL, MVVM and SL4 - in VB.NET

A: 

Hi,

The fact that the Checked command is working correctly means that the attached behavior was correctly implemented. That said, the behavior only monitors a single event which you specify in this line:

AddHandler checkableObject.Checked, AddressOf checkableObject_Checked

Thus, you are subscribing to the Checked event of the Checkbox control. As you want to have control of the Checkbox being unchecked, you simply need to create another attached behavior using the Unchecked event. This assumes that you want a different command executed when the button is checked/unchecked. If you use the same command, then binding to the Command property should suffice.

I hope this helps.

Damian Schenkelman