WPF DependencyProperties

前端 未结 6 2030
孤城傲影
孤城傲影 2021-01-05 18:12

I have just realized I\'ve been coercing binding/dependency properties and not really fundamentally understanding the concept.

Heres the dependency property:

6条回答
  •  耶瑟儿~
    2021-01-05 18:56

    In your code you register the Dependency Property for the class TextBox (last line of quotation).

    public static readonly DependencyProperty ProblemProperty =
    DependencyProperty.Register(
    "Problem",
    typeof(string),
    typeof(TextBox));

    So you can set a value for the ProblemProperty only for textboxes, but I cannot find any textbox in any of the code snippets. You should register your dependency property for the type, to which the value will be assigned, from your sample the right choice is not obvious to me. You could, as Micah does, define it to be a DP of the window, then set the property on your instantiated window. Or you could define it to any named dependency object inside the window, i.e. some object with Name=m_ContentElement, and then set your binding to
    {Binding ElementName=m_ContentElement, Path=Problem}
    or shorter:
    {Binding Problem, ElementName=m_ContentElement}

提交回复
热议问题