从ObserverCollection绑定到ToggleSwitch'IsChecked'

时间:2017-07-09 15:30:20

标签: c# wpf xaml mvvm binding

我尝试从observerCollection绑定到ToggleSwitch(MahApp) 但它不起作用

XAML:

<mah:ToggleSwitch Name="switchLEDA" IsChecked="{Binding ConfParams[0], Mode=TwoWay}" Content="" Grid.Column="2" Grid.Row="2"/>

视图模型:

public ObservableCollection<bool> ConfParams
    {
        get { return _chromaConfigurationModel.ConfParams; }
        set { _chromaConfigurationModel.ConfParams = value; }
    }

型号:

private ObservableCollection<bool> _confParams;
 public ObservableCollection<bool> ConfParams
    {
        get { return _confParams; }
        set { _confParams = value; }
    }

任何人都可以停止我吗?

1 个答案:

答案 0 :(得分:0)

您无法更改boolObservableCollection<bool>的值,因此TwoWay绑定无效。 bool是一种在传递时复制的值类型。

您应该绑定到实际可以设置的属性:

public class ConfParams
{
    public bool Value { get; set; }
}

查看型号:

public ObservableCollection<ConfParams> ConfParams
{
    get { return _chromaConfigurationModel.ConfParams; }
    set { _chromaConfigurationModel.ConfParams = value; }
}

查看:

<mah:ToggleSwitch Name="switchLEDA" IsChecked="{Binding ConfParams[0].Value, Mode=TwoWay}" Content="" Grid.Column="2" Grid.Row="2"/>