如何以编程方式绑定此CheckBox?

时间:2014-06-27 18:27:02

标签: c# wpf data-binding checkbox binding

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

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。有什么问题?

1 个答案:

答案 0 :(得分:1)

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

autoScrollBinding.Source = this;

但是如果你想要从代码中获得更新以反映在你的窗口上,那么你需要实现INotifyProertyChanged作为@evanb提到。

相关问题