2013-05-01 15 views
10

Sto imparando MVVM e WPF. Ho un file xaml nel mio progetto e che ha un semplice gestore di eventi click nel codice sottostante.Comando di binding in WPF con MVVM

Ora voglio fare lo stesso in MVVM. Ho letto molti articoli e ho letto anche molte risposte in sof. Ma ancora incapace di farlo.

Qualcuno può dare un semplice esempio in cui un pulsante di scatto evento è fatto in MVVM.

Modifica

<Window x:Class="WhiteBalance.BaseCalWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:uc="clr-namespace:NumberUpDownControl;assembly=NumberUpDownControl" 
     xmlns:viewn="clr-namespace:WhiteBalance.ViewModels" 
     Title="RefImgSettingWindow" Height="900" Width="1000" ResizeMode="NoResize" 
     BorderThickness="4"> 
    <Window.Resources> 
     <viewn:DashBoardViewModel x:Key="demokey"></viewn:DashBoardViewModel> 
    </Window.Resources> 
    <Grid x:Name="gdParent" DataContext="{StaticResource demokey}"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="490" /> 
      <ColumnDefinition Width="488*" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="300" /> 
      <RowDefinition Height="300" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <StackPanel Grid.Row="0" Grid.Column="0"> 
      <Label Content="{Binding Path=NAME,Mode=TwoWay}" Height="28" Name="lblTest" /> 
      <Button Content="Capture" Height="23" Name="btnCapture" Width="75" Command="{Binding Path=SaveCommand}" 
          Canvas.Left="94" Canvas.Top="254" /> 

     </StackPanel> 
    </Grid> 
</Window> 

namespace WhiteBalance.ViewModels 
{ 
    public class DashBoardViewModel: ObservableObject 
    { 
     private string _name = "dsqdasd"; 

     public string NAME 
     { 
      get { return _name; } 
      set { _name = value; } 
     } 

     public ICommand SaveCommand 
     { 
      get; 
      set; 
     } 

     private bool CanExecuteSaveCommand() 
     { 
      return true; // !string.IsNullOrEmpty(LastName); 
     } 

     private void CreateSaveCommand() 
     { 
      SaveCommand = new RelayCommand(SaveExecute, CanExecuteSaveCommand); 
     } 

     public void SaveExecute() 
     { 
      //Person.Save(_newPerson); 
      NAME = "Changed Name"; 
     } 

     public DashBoardViewModel() 
     { 
      //objModel.TestText = "This will change"; 
      NAME = "TestName"; 
     } 
    } 
} 

Grazie in anticipo.

+0

Si sta utilizzando un framework MVVM? Che cosa hai provato? –

+0

È obbligatorio utilizzare il framework MVVM? Non sono a conoscenza di alcun quadro. L'unica cosa che ho è che ho bisogno di usare ICommand ma come usare questo e quali modifiche sono necessarie in ViewModel, non lo so. – Narendra

+2

No, ma aiuta MOLTO, raccomanderei MVVM Light http://www.galasoft.ch/mvvm/ –

risposta

23

È possibile associare la proprietà Command del pulsante a qualsiasi proprietà che restituisce ICommand. Prism implementa un bel comodo comando chiamato DelegateCommand che è molto facile da usare (here is a knock-off di esso):

public ICommand MyButtonClickCommand 
{ 
    get { return new DelegateCommand<object>(FuncToCall, FuncToEvaluate); } 
} 

private void FuncToCall(object context) 
{ 
    //this is called when the button is clicked 
} 

private bool FuncToEvaluate(object context) 
{ 
    //this is called to evaluate whether FuncToCall can be called 
    //for example you can return true or false based on some validation logic 
    return true; 
} 



<Button x:Name="myButton" Command="{Binding MyButtonClickCommand}" /> 

L'esempio CodeProject How to use Commands in WPF ha un esempio molto simile con il codice che si può facilmente lavorare attraverso. La precedente domanda Overflow dello stack ha un esempio che utilizza RoutedCommands associato a: How to bind Close command to a button e How to bind WPF button to a command in ViewModelBase? ha un esempio leggermente più avanzato.

+0

Grazie a @slugster. DelegateCommand è una classe definita dal sistema o devo creare questo? Sto controllando i collegamenti forniti da te. – Narendra

+0

@Narendra È definito nelle librerie Prism, è sufficiente fare riferimento a loro, o guardare il link alla versione knock-off. – slugster

+0

Ho aggiunto le modifiche recenti in questione. Puoi spiegare perché non sta chiamando la funzione richiesta? – Narendra