Avalonia: FindControl returns null for UserControl lookup.

Created on 29 Dec 2016  路  2Comments  路  Source: AvaloniaUI/Avalonia

I am getting null when I want to reference UserControl in code behind and using FindControl("Ctrl") to lookup for it. UserControl is just default implementation from Avalonia template.

XAML:
`<Window xmlns="https://github.com/avaloniaui"
xmlns:local="clr-namespace:AvaloniaApplication1;assembly=AvaloniaApplication1"
MinWidth="500" MinHeight="300">

<local:UserControl1 Name="Ctrl"/>

</Window>`

Code:
`namespace AvaloniaApplication1
{
public class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
App.AttachDevTools(this);

        var ctl = this.FindControl<UserControl1>("Ctrl");
    }

    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
    }
}

}`

bug

Most helpful comment

I've been looking into this in WPF and the results are interesting. If I do this (omitting non-relevant attributes etc):

UserControl1.xaml:

<UserControl x:Class="WpfApplication4.UserControl1" Name="foo">

MainWindow1.xaml:

<Window>
    <local:UserControl1/>
</Window>

Then foo is added to UserControl1's name scope but not MainWindows.

If however I do this:

UserControl1.xaml:

<UserControl x:Class="WpfApplication4.UserControl1" Name="foo">

MainWindow1.xaml:

<Window>
    <local:UserControl1 x:Name="bar"/>
</Window>

Then bar is added to both UserControl1s and MainWindows name scope. I think this is because in WPF simply setting a Name on a control doesn't add it to a name scope: you need to call RegisterName.

I didn't want this behavior in Avalonia because I didn't like the fact that there was different behavior between controls created via XAML and controls created in code, but it seems that the behavior has a use in this case.

We have two options here:

  1. revert to WPF's behavior (which I'm not even sure OmniXaml supports tbh)
  2. register the control in both name scopes

I lean towards 2. though this would cause problems if one were to e.g. include a named UserControl twice in a window.

All 2 comments

Thanks for the report! This appears to be a problem with named UserControls: seems that becase the UserControl is itself a name scope, the control is getting added to its own name scope and not that of the parent control.

I've been looking into this in WPF and the results are interesting. If I do this (omitting non-relevant attributes etc):

UserControl1.xaml:

<UserControl x:Class="WpfApplication4.UserControl1" Name="foo">

MainWindow1.xaml:

<Window>
    <local:UserControl1/>
</Window>

Then foo is added to UserControl1's name scope but not MainWindows.

If however I do this:

UserControl1.xaml:

<UserControl x:Class="WpfApplication4.UserControl1" Name="foo">

MainWindow1.xaml:

<Window>
    <local:UserControl1 x:Name="bar"/>
</Window>

Then bar is added to both UserControl1s and MainWindows name scope. I think this is because in WPF simply setting a Name on a control doesn't add it to a name scope: you need to call RegisterName.

I didn't want this behavior in Avalonia because I didn't like the fact that there was different behavior between controls created via XAML and controls created in code, but it seems that the behavior has a use in this case.

We have two options here:

  1. revert to WPF's behavior (which I'm not even sure OmniXaml supports tbh)
  2. register the control in both name scopes

I lean towards 2. though this would cause problems if one were to e.g. include a named UserControl twice in a window.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

khoshroomahdi picture khoshroomahdi  路  4Comments

MiKaMaru picture MiKaMaru  路  4Comments

danwalmsley picture danwalmsley  路  4Comments

CreateLab picture CreateLab  路  3Comments

grokys picture grokys  路  4Comments