开发者

WPF Binding Command

I have following checkbox:

<dxe:CheckEdit Margin="2,0" IsChecked="{Binding SelectedContact.isMajor,Mode=TwoWay,Co开发者_如何学运维nverter={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


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();
    }
}


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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜