WPF - FindName Returns null when it should not

后端 未结 8 750
-上瘾入骨i
-上瘾入骨i 2020-12-09 07:48

FindName is broken for me :(

The object I am looking for is there. I have proof.

Here is the scenario:

ToggleButton button = (ToggleButton)s         


        
8条回答
  •  情话喂你
    2020-12-09 08:13

    In my experience, this happens when you add items via code-behind. I've found that you can fool FindName() (or the animation framework) via name scopes. That is, when you create your control, you do

        NameScope.GetNameScope(yourContainer).RegisterName("name of your control", yourControlInstance);
    

    For this to work reliably, though, you must make sure that you unregister the name:

        NameScope.GetNameScope(yourContainer).UnregisterName("name of your control");
    

    Posting this for future reference.

提交回复
热议问题