复选框命令,选中-确定,取消选中不确定,如何绑定取消选中命令?
我在将取消选中命令附加到复选框时遇到问题。或者更正确的是,我不知道如何编码。这是我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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:
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.