Silverlight 中选中/取消选中复选框的 MVVM-Light EventToCommand 行为

发布于 2024-10-11 03:12:48 字数 1578 浏览 1 评论 0原文

我想处理 Checkbox 控件的 Checked 和 Unchecked 事件并在 ViewModel 中执行命令。我为 Checked 和 Unchecked 事件连接了一个 EventTrigger,如下所示:

<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>

我在 ViewModel 中定义了一个 RelayCommand 并为其连接了一个操作:

public RelayCommand<Boolean> SetCloseTime{ get; private set; }

   ...

SetCloseTime= new RelayCommand<bool>(ExecuteSetCloseTime);

该命令的操作中的参数始终解析为 CheckBox 的先前状态,例如 false当 CheckBox 被选中时为 true,当 CheckBox 未被选中时为 true。

void ExecuteSetCloseTime(bool isChecked) 
{
    if (isChecked)
    {
        // do something
    }
}

这是预期的行为吗?

我有一个解决方法,其中我为“已选中”和“未选中”设置单独的触发器(和命令),并使用 RelayCommand 而不是 RelayCommand。当复选框被选中和取消选中时,每个命令都会正确执行。不过感觉有点脏——甚至比我的 ViewModel 中的 UI 代码还脏:)

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

计㈡愣 2024-10-18 03:12:48

我认为使用“Click”事件而不是“Checked”或“UnChecked”只需一个命令即可解决此问题,无需额外的代码。
在 XAML 中,它看起来像,

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

现在其余的代码应该可以按您想要的方式工作,

谢谢,

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,

小…红帽 2024-10-18 03:12:48

为什么不在 Schedule.Is24Hour 中处理您的操作。在 setter 中,您始终可以看到该属性何时更改。

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

樱花落人离去 2024-10-18 03:12:48

我这样做是为了检查

复选框

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

模型视图视图的

 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}

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