WPF 绑定命令
我有以下复选框:
<dxe:CheckEdit Margin="2,0" IsChecked="{Binding SelectedContact.isMajor,Mode=TwoWay,Converter={StaticResource CheckBoxNullToFalse}}">More than 18</dxe:CheckEdit>
这就是我想要实现的目标:当用户单击该复选框时,我想调用一个函数,但也分配 isMajor 字段。
我看到如何执行此操作的唯一方法是绑定到将执行这两个操作的命令
是否有更直接的方法?
谢谢
约翰
I have following checkbox:
<dxe:CheckEdit Margin="2,0" IsChecked="{Binding SelectedContact.isMajor,Mode=TwoWay,Converter={StaticResource CheckBoxNullToFalse}}">More than 18</dxe:CheckEdit>
This is what i want to achieve: when the user clicks on the check box, i want to call a function but also assign the isMajor field.
The only way i see how to do this is to bind to a command that will perform both operations
Is there a more straightforward way ?
Thanks
John
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
isMajor
设为属性而不是字段,并在 setter 中调用方法Make
isMajor
a property rather than a field, and call a method in the setter老实说,使用命令是我首先想到的事情。我假设 dxe:CheckEdit 是 CheckBox 的一些变体 - 您可能会发现 ToggleButton 很有用,作为替代方案,具体取决于您正在寻找的行为。
我不确定你所说的“直接”是什么意思(最少的代码,最容易理解等),但是将
IsChecked
绑定到isMajor
属性(根据 @Thomas Levesque)并绑定到命令(中继 或 Delegate,例如)调用该函数提供了一种干净的方法来完成您想要的两件事,而不会像您那样在代码中引入副作用例如,如果您从属性设置器或Converter
调用该函数,这对于其他开发人员来说并不明显。或者对你来说,当你一年后回来维护这个时。 :)To be honest, using a Command is the first thing that occurred to me. I assume that
dxe:CheckEdit
is some CheckBox variant - you might find aToggleButton
useful, as an alternative, depending on what behavior you're looking for.I'm not sure what you mean by "straightforward" (least code, easiest to understand, etc.), but binding
IsChecked
to anisMajor
property (as per @Thomas Levesque) and binding to a Command (Relay or Delegate, for example) to call the function provides a clean way to do both things that you want without introducing side-effects into your code, as you would if you called the function from a property setter or yourConverter
, for example, that wouldn't be obvious to other developers. Or to you, when you return to maintain this a year from now. :)