复选框命令,选中-确定,取消选中不确定,如何绑定取消选中命令?

发布于 2024-09-28 14:07:35 字数 2990 浏览 0 评论 0原文

我在将取消选中命令附加到复选框时遇到问题。或者更正确的是,我不知道如何编码。这是我的 check 命令的代码,它应该如何才能使 uncheck 也正常工作?

视图:

<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

命令:

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

如上所述,检查命令工作正常,并且连接到它的命令和方法被触发,我需要做什么才能使取消检查也正常工作? 有关信息,我在 VB.NET 中使用 PRISM、CAL、MVVM 和 SL4

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮萍、无处依 2024-10-05 14:07:35

Checked 命令正常工作这一事实意味着附加行为已正确实现。也就是说,该行为仅监视您在这一行中指定的单个事件:

AddHandler checkableObject.Checked, AddressOf checkableObject_Checked

因此,您正在订阅 Checked 复选框控件的事件。由于您希望控制未选中的复选框,因此您只需使用 未选中 事件。这假设您希望在选中/取消选中按钮时执行不同的命令。如果您使用相同的命令,则绑定到 Command 属性就足够了。

我希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文