MVVM-Light EventToCommand Behavior for CheckBox Ch

2019-08-08 20:57发布

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

3条回答
地球回转人心会变
2楼-- · 2019-08-08 21:38

i do this for checking checkbox

for view

 <CheckBox Margin="126,0,0,0"  IsChecked="{Binding UseNOCODE, Mode=TwoWay}" Content="Reply Messages ?" />

for the modelview

 private bool _useNOCODE = false;
    public bool UseNOCODE
    {
        get
        {
            return _useNOCODE;
        }

        set
        {
            if (_useNOCODE == value)
            {
                return;
            }
            _useNOCODE = value;
            RaisePropertyChanged("UseNOCODE");
            UseNoCodeChecked();
        }
    }
 private void UseNoCodeChecked()
    {//check the properties and what you like}
查看更多
Animai°情兽
3楼-- · 2019-08-08 21:45

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,

<i:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand CommandParameter="{Binding IsChecked, ElementName=chkIsExtendedHr}" Command="{Binding Path=SetCloseTime, Mode=OneWay}" />
</i:EventTrigger> 

now rest of you code should work the you wanted,

thanks,

查看更多
ゆ 、 Hurt°
4楼-- · 2019-08-08 21:50

Why don't you handle your actions in your Schedule.Is24Hour. In setter you always can see when that property is changed.

查看更多
登录 后发表回答