How do I use WPF bindings with RelativeSource?

前端 未结 13 1958
执念已碎
执念已碎 2020-11-22 03:00

How do I use RelativeSource with WPF bindings and what are the different use-cases?

相关标签:
13条回答
  • 2020-11-22 03:54

    Some useful bits and pieces:

    Here's how to do it mostly in code:

    Binding b = new Binding();
    b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, this.GetType(), 1);
    b.Path = new PropertyPath("MyElementThatNeedsBinding");
    MyLabel.SetBinding(ContentProperty, b);
    

    I largely copied this from Binding Relative Source in code Behind.

    Also, the MSDN page is pretty good as far as examples go: RelativeSource Class

    0 讨论(0)
提交回复
热议问题