代码之家  ›  专栏  ›  技术社区  ›  Shawn Mclean

将Silverlight UI属性绑定到源

  •  0
  • Shawn Mclean  · 技术社区  · 16 年前

    我有一个复选框,需要绑定到源中的一个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);
    }
    

    当我通过选中和取消选中复选框来测试它时,它不会改变ismapediton。

    2 回复  |  直到 16 年前
        1
  •  0
  •   Robin    16 年前

    最简单的方法是将 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);
    }
    
        2
  •  0
  •   Jeff Atwood    16 年前

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

    public bool? IsChecked { get; set; }