簡體   English   中英

如何以編程方式綁定此CheckBox?

[英]How can I programmatically bind this CheckBox?

這段代碼在我的類構造函數中:

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。 問題是什么?

您應該將“ 源”設置為“相對源”。

autoScrollBinding.Source = this;

但是,如果您希望代碼更新反映在窗口中,則需要實現INotifyProertyChanged,如@evanb所述。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM