Binding properties in code behind

前端 未结 1 630
南方客
南方客 2020-12-02 02:17

I have WPF application and a window in it. Lets have something like this in my xml:

相关标签:
1条回答
  • 2020-12-02 02:42

    Here's how you define and apply a binding in code:

    Binding binding = new Binding {
      Source = TitleLabel,
      Path = new PropertyPath("Content"),
    };
    BottomLabel.SetBinding(ContentControl.ContentProperty, binding);
    

    Note that on objects that don't derive from FrameworkElement, you have to explicitly use BindingOperations.SetBinding() instead of element.SetBinding():

    BindingOperations.SetBinding(BottomLabel, ContentControl.ContentProperty, binding);
    
    0 讨论(0)
提交回复
热议问题