WPF CheckBox 的选中和未选中状态的单独事件:为什么?
是否有一个像
Changed
这样的事件可以用来同时处理这两个事件?为什么他们要分开?
是否因为两者都有一个事件需要您按名称引用控件,您需要在 XAML 中指定该名称,这会增加混乱吗?
Is there a single event like
Changed
that I can use to handle both events together?Why are they separated like this?
Is it because having a single event for both would requires you to reference the control by name, which you would need to specify in the XAML, and this would increase the clutter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sender
参数的IsChecked
属性(在将其转换为CheckBox
或 <当然是代码>ToggleButton)。EventTriggers
无法区分状态,只能通过事件来区分,因此需要两个不同的事件。一般而言:我根本不会使用这些事件 - 我会将
IsChecked
属性绑定到ViewModel
上的适当属性,将代码隐藏到最小值(理想情况下根本没有自定义代码)。IsChecked
property of thesender
parameter (after casting it toCheckBox
orToggleButton
of course).EventTriggers
and similar.EventTriggers
can't distinguish between state, only by event, so two different events are needed.On a general note: I wouldn't use the events at all - I would bind the
IsChecked
property to an appropiate property on yourViewModel
, keeping your code-behind to a minimum (Ideally no custom code at all).这种拆分为那些需要它的人提供了更多的粒度(不会对那些不需要它的人造成伤害),并且如果您愿意,您可以使用一个处理程序来处理这两个事件。
The split gives more granularity for those who need it (can't hurt for those who don't) and if you want you can handle both events with one handler.
例如,在选中时启动故事板并在未选中时停止它。
For example to start a storyboard when checked and stop it when unchecked.