Wie kann ich mit mehreren Checkboxen in das MVVM-pattern?

Bindung checkbox in WPF ist häufiges Problem, aber ich bin immer noch nicht finden, Beispiel-code, die leicht zu Folgen für Anfänger. Ich habe das Kontrollkästchen Liste im WPF-wählen Sie Lieblings-Sport' Namen. Die Anzahl der Checkboxen ist statisch in meinem Fall. Kann jemand mir zeigen, wie zu implementieren ViewModel für dieses Problem?

FavoriteSportsView.xaml:

  <StackPanel Height="50" HorizontalAlignment="Left" VerticalAlignment="Top" 
  Width="150">

  <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"  
  Command="{Binding Path=SportsResponseCommand}" 
  CommandParameter="Football" 
  Content="Football" 
  Margin="5" />

  <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" 
  Command="{Binding Path=SportsResponseCommand}" 
  CommandParameter="Hockey" 
  Content="Hockey" 
  Margin="5" />

  <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" 
  Command="{Binding Path=SportsResponseCommand}" 
  CommandParameter="Golf" 
  Content="Golf" 
  Margin="5" />
  </StackPanel>

FavoriteSportsViewModel.cs

  public class FavoriteSportsViewModel.cs {

    //Since I am using the same IsChecked in all check box options, I found all check 
    //boxes gets either checked or unchecked when I just check or uncheck one option.
    //How do i resolve this issue? I don't think i need seprate IsChecked for each 
    //check box option.

    private bool _isChecked;
    public bool IsChecked{
      get {
           return _isChecked;
       }

      set { if (value != _isChecked) 
             _isChecked = value;
            this.OnPropertyChanged("IsChecked");
       }
    }


    //How do i detect parameter in this method?
    private ICommand _sportsResponseCommand;
    public ICommand SportsResponseCommand
    {
        get
        {
            if (_sportsResponseCommand== null)
                _sportsResponseCommand= new
                    RelayCommand(a => DoCollectSelectedGames(), p => true);
            return _sportsResponseCommand;
        }
        set
        {
            _sportsResponseCommand= value;

        }

    }

    private void DoCollectSelectedGames(){ 
      //Here i push all selected games in an array
    }


    public abstract class ViewModelBase : INotifyPropertyChanged
    {
       public event PropertyChangedEventHandler PropertyChanged;
       public void OnPropertyChanged(string propertyName)
       {
         if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
       }
    }

  }

Ich bin mir nicht sicher, wie Sie die folgenden im oben ViewModel:
1. Wie implementiere ich eine einzige Methode, um alle meine Optionen?
2. wie erkenne ich die jeweils eines der Kontrollkästchen, um zu sehen, ob geprüft oder nicht
3. Wie kann ich utlize CommandParameter?
4. Wie implementiere ich SportsResponseCommand richtig

  • Ist über eine checkbox-Liste eine option für dich?
InformationsquelleAutor Shai | 2011-09-26
Schreibe einen Kommentar