0

In Xamarin, I'm trying to use a SwitchCell in a list view with two-way binding.

ObservableCollection<DTC> DATAC = new ObservableCollection<DTC>();
ListView lvDecks = new ListView();
DataTemplate dtDecks = new DataTemplate(typeof(SwitchCell));
dtDecks.SetBinding(SwitchCell.TextProperty, new Binding("Title"));
dtDecks.SetBinding(SwitchCell.OnProperty, new Binding("Chosen",BindingMode.TwoWay));
lvDecks.ItemsSource = DATAC;
lvDecks.ItemTemplate = dtDecks;

The Switch seems to work if I set the Chosen value externally but the Switch doesn't change the Chosen value. The binding does not appear to be two-way; what did I miss?

Here's the definition of DTC:

public class DTC : INotifyPropertyChanged       //Deck, Title, Chosen
{
    public event PropertyChangedEventHandler PropertyChanged;

    ApplicationProperties ap = new ApplicationProperties();

    private string _deck;  //resource name
    private string _title;
    private bool _chosen;

    public string Deck
    {
        get { return this._deck; }
    }

    public string Title
    {
        get { return this._title; }
    }

    public Boolean Chosen
    {
        set
        {
            if (this._chosen != value)
            { 
                this._chosen = value;
                OnPropertyChanged("Chosen");
            }
        }

        get { return this._chosen; }
    }

    public DTC(string ADeck, string ATitle) //cons
    {
        _deck = ADeck;
        _title = ATitle;

        _chosen = true; //(bool)ap.ReadSettings(_deck, false);
        OnPropertyChanged("Chosen");
    }

    void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}//class

And then lastly I'll need to set up an OnChanged event for the SwitchCell to update some other things when I switch. But it doesn't look like that should be a SetBinding on dtDecks - so where does THAT go?

Thanks

user3693290
  • 75
  • 1
  • 7

1 Answers1

1

I think you can try to use Switch instead of SwitchCell and Bind IsToggledProperty

Alessandro Caliaro
  • 5,623
  • 7
  • 27
  • 52