在 Xamarin 列表视图中双向绑定 SwitchCell



在Xamarin中,我尝试在具有双向绑定的列表视图中使用SwitchCell。

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;

如果我在外部设置"已选择"值,则开关似乎可以工作,但开关不会更改"已选择"值。绑定似乎不是双向的;我错过了什么?

以下是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

最后,我需要为 SwitchCell 设置一个 OnChanged 事件,以便在切换时更新其他一些内容。但它看起来不应该是dtDecks上的SetBinding - 那么它去哪里了?

谢谢

我认为您可以尝试使用Switch而不是SwitchCell和Bind IsToggledProperty

最新更新