Silverlight 中选中/取消选中复选框的 MVVM-Light EventToCommand 行为
我想处理 Checkbox 控件的 Checked 和 Unchecked 事件并在 ViewModel 中执行命令。我为 Checked 和 Unchecked 事件连接了一个 EventTrigger,如下所示:
<CheckBox x:Name="chkIsExtendedHr" IsChecked="{Binding Schedule.Is24Hour, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<GalaSoft_MvvmLight_Command:EventToCommand
CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}"
Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<GalaSoft_MvvmLight_Command:EventToCommand
CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}"
Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
我在 ViewModel 中定义了一个 RelayCommand 并为其连接了一个操作:
public RelayCommand<Boolean> SetCloseTime{ get; private set; }
...
SetCloseTime= new RelayCommand<bool>(ExecuteSetCloseTime);
该命令的操作中的参数始终解析为 CheckBox 的先前状态,例如 false当 CheckBox 被选中时为 true,当 CheckBox 未被选中时为 true。
void ExecuteSetCloseTime(bool isChecked)
{
if (isChecked)
{
// do something
}
}
这是预期的行为吗?
我有一个解决方法,其中我为“已选中”和“未选中”设置单独的触发器(和命令),并使用 RelayCommand
而不是 RelayCommand
。当复选框被选中和取消选中时,每个命令都会正确执行。不过感觉有点脏——甚至比我的 ViewModel 中的 UI 代码还脏:)
谢谢
I would like to handle the Checked and Unchecked events of a Checkbox control and execute a command in my ViewModel. I wired up an EventTrigger for both the Checked and Unchecked events as follows:
<CheckBox x:Name="chkIsExtendedHr" IsChecked="{Binding Schedule.Is24Hour, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<GalaSoft_MvvmLight_Command:EventToCommand
CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}"
Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<GalaSoft_MvvmLight_Command:EventToCommand
CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}"
Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
I defined a RelayCommand in my ViewModel and wired up an action for it:
public RelayCommand<Boolean> SetCloseTime{ get; private set; }
...
SetCloseTime= new RelayCommand<bool>(ExecuteSetCloseTime);
The parameter in the action for the command always resolves to the previous state of the CheckBox, e.g. false when the CheckBox is checked, and true when the CheckBox is unchecked.
void ExecuteSetCloseTime(bool isChecked)
{
if (isChecked)
{
// do something
}
}
Is this expected behavior?
I have a workaround where I have separate triggers (and commands) for the Checked and Unchecked and use a RelayCommand
instead of RelayCommand<bool>
. Each command executes correctly when the CheckBox is checked and unchecked. Feels a little dirty though - even dirtier than having UI code in my ViewModel :)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为使用“Click”事件而不是“Checked”或“UnChecked”只需一个命令即可解决此问题,无需额外的代码。
在 XAML 中,它看起来像,
现在其余的代码应该可以按您想要的方式工作,
谢谢,
I think using "Click" event instead of "Checked" or "UnChecked" can solve this problem with just one command and no additional code.
In XAML it will look like,
now rest of you code should work the you wanted,
thanks,
为什么不在 Schedule.Is24Hour 中处理您的操作。在 setter 中,您始终可以看到该属性何时更改。
Why don't you handle your actions in your Schedule.Is24Hour. In setter you always can see when that property is changed.
我这样做是为了检查
复选框
模型视图视图的
i do this for checking checkbox
for view
for the modelview