开发者

Binding silverlight UI property to source

I have a checkbox which i need to bind to a bool in the source and also disable or enable a container.

My source is as follows for the binding but it does not work:

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, ma开发者_开发问答pEditBind);
    //Bind to disable children (point and area buttons).
    EditBtnContainer.SetBinding(IsEnabledProperty, mapEditBind);
}

When I test this by checking and unchecking the checkbox, it does not altar isMapEditOn.


The easiest way to do this is to wrap the isMapEditOn in a property. If you want change notification from the source you'll need to implement INotifyPropertyChanged (not shown here. See this page for info on implementing this interface ):

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);
}


Try using the type bool? instead of bool. The IsChecked property is defined as:

public bool? IsChecked { get; set; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜