如何以编程方式绑定此复选框
本文关键字:绑定 复选框 方式 编程 | 更新日期: 2023-09-27 18:10:39
这段代码在我的类构造函数中:
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永远不会被调用。有什么问题吗?
你应该设置Source而不是Relative Source
autoScrollBinding.Source = this;
但是如果你想从代码更新得到反映在你的窗口,那么你需要实现INotifyProertyChanged @evanb提到。