WPF - FindName Returns null when it should not

后端 未结 8 761
-上瘾入骨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:16

    Little late to the party (and not actually answer to OP question), but

    when you add elements dynamically, they are not findable by FindName. You need to register them by calling RegisterName.

    Example:

    Button myButton = new Button();
    myButton.Content = generateNumber();
    myButton.Name = "button_" + generateNumber;
    
    RegisterName(myButton.Name, myButton);
    
    Panel.Children.Add(myButton);
    object o = Panel.FindName(myButton.Name);
    

    Maybe someone might find this useful.

提交回复
热议问题