WPF 绑定命令

发布于 2024-10-01 10:39:09 字数 344 浏览 0 评论 0原文

我有以下复选框:

<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 技术交流群。

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

发布评论

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

评论(2

送舟行 2024-10-08 10:39:09

isMajor 设为属性而不是字段,并在 setter 中调用方法

private bool _isMajor;
public bool IsMajor
{
    get { return _isMajor; }
    set
    {
        _isMajor = value;
        OnPropertyChanged("IsMajor");
        DoSomething();
    }
}

Make isMajor a property rather than a field, and call a method in the setter

private bool _isMajor;
public bool IsMajor
{
    get { return _isMajor; }
    set
    {
        _isMajor = value;
        OnPropertyChanged("IsMajor");
        DoSomething();
    }
}
旧竹 2024-10-08 10:39:09

老实说,使用命令是我首先想到的事情。我假设 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 a ToggleButton 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 an isMajor 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 your Converter, for example, that wouldn't be obvious to other developers. Or to you, when you return to maintain this a year from now. :)

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