将silverlight UI属性绑定到源

时间:2010-02-04 21:59:37

标签: silverlight

我有一个复选框,我需要绑定到源中的bool,并禁用或启用容器。

我的来源如下所示,但它不起作用:

private bool isMapEditOn = false;

OnLoadFunction()
{
    //Bindings
    Binding mapEditBind = new Binding("IsChecked") { Source = isMapEditOn, Mode = BindingMode.TwoWay };

    //Bind to check or uncheck the mapEdit Checkbox
    ChckEditMap.SetBinding(ToggleButton.IsCheckedProperty, mapEditBind);
    //Bind to disable children (point and area buttons).
    EditBtnContainer.SetBinding(IsEnabledProperty, mapEditBind);
}

当我通过选中并取消选中复选框来测试时,它不会是geMapEditOn。

2 个答案:

答案 0 :(得分:0)

尝试使用bool类型?而不是布尔。 IsChecked属性定义为:

public bool? IsChecked { get; set; }

答案 1 :(得分:0)

最简单的方法是将isMapEditOn包装在属性中。如果您想要从源代码更改通知,则需要实现INotifyPropertyChanged(此处未显示。有关实现此接口的信息,请参阅this页面):

private bool _isMapEditOn = false;

public bool IsMapEditOn
{
    get
    {
        return _isMapEditOn;
    }
    set
    {
        _isMapEditOn = value;
    }
}

OnLoadFunction()
{
    //Bindings
    Binding mapEditBind = new Binding("IsMapEditOn") { Source = this, Mode = BindingMode.TwoWay };

    //Bind to check or uncheck the mapEdit Checkbox
    ChckEditMap.SetBinding(ToggleButton.IsCheckedProperty, mapEditBind);
    //Bind to disable children (point and area buttons).
    EditBtnContainer.SetBinding(IsEnabledProperty, mapEditBind);
}