Databinding to CLR property in code-behind

前端 未结 5 1339
隐瞒了意图╮
隐瞒了意图╮ 2021-02-15 20:18

Binding to a Dependency Property is easy in code-behind. You just create a new System.Windows.Data.Binding object, and then call the target dependency object\'s

5条回答
  •  离开以前
    2021-02-15 21:05

    You can bind a CLR property to a DepenencyProperty on a control, for example. In this cae, the CLR property is the SOURCE of the binding and the DependencyProperty is the TARGET of the binding. For it to work, the class with the CLR property has to implement INotifyPropertyChanged.

    Here's how you do it in the code behind:

    Binding canModifyBinding = new Binding();
    canModifyBinding.Source = LastRecord;
    canModifyBinding.Path = new PropertyPath( "CanModify" );
    BindingOperations.SetBinding( this, CanModifyProperty, canModifyBinding );
    

    In this case, the Binding object represents the information about the Source: What object is the source, which property of that object is the one you're interested in. BindingOperations.SetBinding is a static method which specifies which DependencyProperty on which DependencyObject is the target of the binding (arguments 2 & 1, respectively), and the Binding to use to get at the source.

    HTH

    Tony

提交回复
热议问题