【问题标题】:How can I programmatically bind this CheckBox?如何以编程方式绑定此 CheckBox?
【发布时间】:2014-08-18 23:07:43
【问题描述】:

这段代码在我的类构造函数中:

CheckBox autoScrollCheckBox = new CheckBox();
autoScrollCheckBox.VerticalAlignment = VerticalAlignment.Center;
autoScrollCheckBox.Content = "Enable";
Binding autoScrollBinding = new Binding();
autoScrollBinding.Path = new PropertyPath("AutoScrollingIsEnabled");
autoScrollBinding.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
autoScrollBinding.Mode = BindingMode.TwoWay;
autoScrollCheckBox.SetBinding(CheckBox.IsCheckedProperty, autoScrollBinding);
autoScrollBox.Content = autoScrollCheckBox;

这是同一类:

public bool AutoScrollingIsEnabled
{
    get
    {
        return !autoScrollingIsPaused;
    }
    set
    {
        autoScrollingIsPaused = !value;
    }
}

但永远不会调用 AutoScrollingIsEnabled。有什么问题?

【问题讨论】:

  • 您是否正在实施 INotifyPropertyChanged?
  • 我发现输出窗口对绑定问题非常有帮助。

标签: c# wpf data-binding checkbox binding


【解决方案1】:

您应该设置Source 而不是相对源。

autoScrollBinding.Source = this;

但是,如果您希望代码更新反映在您的窗口上,那么您需要按照@evanb 提到的那样实现 INotifyProertyChanged。

【讨论】:

    猜你喜欢
    • 2011-02-02
    • 2014-02-25
    • 1970-01-01
    • 2012-12-03
    • 2011-10-22
    • 1970-01-01
    • 2015-10-12
    • 2011-07-30
    • 1970-01-01
    相关资源
    最近更新 更多