question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FindControl returns null for UserControl

See original GitHub issue

Hi,

I’ve updated to the latest CI build that uses XAMLIL.

Now the FindControl if used inside the ctor returns null for UserControl example:

    public ImageColorReplacePage()
    {
        this.InitializeComponent();
        this.Initialized += (s, e) =>
        {
            // FindControl after the Initialization is complete works
            var thisWontBeNull = this.FindControl<KitItemInformationView>("kitView");
        };

        // FindControl within the Constructor won't find anything
        var thisOneWillBeNull = this.FindControl<KitItemInformationView>("kitView");
    }

Repro project: https://github.com/braca/avalonia-findcontrol-bug

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Kermaliscommented, Jun 13, 2019

For anyone who wants a workaround, here’s a method you can use:

        private void Find()
        {
            //var thing = this.FindControl<MyUserControl>("TheName");
            var thing = this.TemporaryFix<MyUserControl>("TheName");
        }

        // Temporary fix for https://github.com/AvaloniaUI/Avalonia/issues/2562
        private T TemporaryFix<T>(string name) where T : UserControl
        {
            T Recursion(System.Collections.Generic.IEnumerable<Avalonia.LogicalTree.ILogical> list)
            {
                foreach (Avalonia.LogicalTree.ILogical i in list)
                {
                    if (i is Avalonia.INamed named && named is T ret && ret.Name == name)
                    {
                        return ret;
                    }
                    else
                    {
                        T r = Recursion(i.LogicalChildren);
                        if (r != null)
                        {
                            return r;
                        }
                    }
                }
                return null;
            }
            return Recursion(this.LogicalChildren);
        }

You would put TemporaryFix<T>() in your class since this.LogicalChildren is actually protected for UserControls. You could put it in some utils class if you pass this.LogicalChildren as another parameter instead.

0reactions
Slxdycommented, May 31, 2022

Should be fixed by #2705

Unfortunately, it isn’t #8231

Read more comments on GitHub >

github_iconTop Results From Across the Web

this.Parent.FindControl returns NULL when called from a ...
On one of it's click events, I call (Label)this.Parent.FindControl("lblRequestNumber"); and it works ok, returning the label that I want.
Read more >
Page.FindControl() Returning Null Issues and Solutions ...
A developer without understanding the situation could assume FindControl() will return a control found within that page. This is completely ...
Read more >
Nested controls in UserControl return null (FindControl) - Wrox
I have a UserControl that has some controls inside. I want to refer those controls after, in another postback. But when I try...
Read more >
FindControl null - is there a way to identify control ID's?
FindControl ("TextBoxA") it will return null because it doesn't "own" any controls named TextBoxA. If you want to traverse the control hierarchy, ...
Read more >
On page _load find control recursive function return null
i use this function for find control on page its working fine on button click but not working on page load its return...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found