【问题标题】:Binding silverlight UI property to source将 silverlight UI 属性绑定到源
【发布时间】:2010-02-05 12:11:31
【问题描述】:

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

我的绑定源如下,但它不起作用:

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。

【问题讨论】:

    标签: silverlight


    【解决方案1】:

    最简单的方法是将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】:

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

      public bool? IsChecked { get; set; }
      

      【讨论】:

      • 我更改了私有 bool isMapEditOn = false;到私人布尔? isMapEditOn = false;如果这就是你所说的,它就不起作用了。
      猜你喜欢
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-28
      相关资源
      最近更新 更多