在Xamarin Listview中双向绑定SwitchCell

时间:2017-05-26 15:14:04

标签: c# listview xamarin two-way

在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;

如果我在外部设置选择值但Switch没有更改选择值,则Switch似乎有效。 绑定似乎不是双向的;我错过了什么?

这里是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 - 那么它会去哪里?

由于

1 个答案:

答案 0 :(得分:1)

我认为您可以尝试使用Switch代替SwitchCell并绑定IsToggledProperty