ToggleButton绑定ViewModel

时间:2017-10-12 18:20:50

标签: binding viewmodel togglebutton

您好我想将Togglebutton与ViewModel绑定。以下是视图代码

 <ToggleButton Content="Toggle" Width="80">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <i:InvokeCommandAction Command="{Binding Path=CheckedCommand}"/>
                                    </i:EventTrigger>
                                    <i:EventTrigger EventName="Unchecked">
                                        <i:InvokeCommandAction Command="{Binding Path=UncheckedCommand}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </ToggleButton>

任何人都可以帮助我使用相应的ViewModel代码吗?

1 个答案:

答案 0 :(得分:0)

你在使用棱镜吗?如果没有,您还需要在this或BaseViewModel类中实现DelegateCommand。

    public class YourViewModel
    {
        private DelegateCommand _checkedCommand;
        public DelegateCommand CheckedCommand
        {
            get
            {
                return _checkedCommand ?? (_checkedCommand = new DelegateCommand(OnCheckedCommand));
            }
        }

        private DelegateCommand _uncheckedCommand;
        public DelegateCommand UncheckedCommand
        {
            get
            {
                return _uncheckedCommand ?? (_uncheckedCommand = new DelegateCommand(OnUncheckedCommand));
            }
        }

        private void OnUncheckedCommand() { }

        private void OnCheckedCommand() { }
    }