2017-10-12 114 views
-1

嗨我想用ViewModel綁定一個Togglebutton。以下是查看代碼ToggleButton Binding 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代碼?

回答

0

您是否在使用棱鏡?如果沒有,您還需要在此或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() { } 
    }