anton.sodergren_pdx
Topic Author
Posts: 18
Joined: 24 Mar 2022, 17:10

Namescopes in nested UserControls

26 May 2023, 13:03

Hello!
I'm running into an issue with namescopes in UserControls. In particular, I'm using a UserControl of one type inside of a UserControl of another type, and both of the root nodes are named "Root", which causes the issues.
Here's a minimal example, with one UserControl called "ChildControl" with a property named "ChildControlName", and one UserControl called "ParentControl" with a property named "ParentControlName". The child UserControl:
<UserControl x:Class="Game.ChildControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Name="Root">
    <TextBlock Text="{Binding ElementName=Root, Path=ChildControlName}"/>
</UserControl>
So far it's all fine.
Now for the parent UserControl:
<UserControl x:Class="Game.ParentControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:game="clr-namespace:Game"
             x:Name="Root">
    <game:ChildControl ChildControlName="{Binding ElementName=Root, Path=ParentControlName}"/>            
</UserControl>
Here I would expect the binding
ChildControlName="{Binding ElementName=Root, Path=ParentControlName}"
to bind to a property inside of ParentControl, but the binding fails because Noesis tries to look for the property inside of ChildControl instead, and produces an error message saying that it can't find the property ParentControlName in ParentControl. Is it really intended that it would look for the name "Root" in the child element's namescope before looking in the parent's namescope? From my understanding, the child's namescope should only apply inside of the XAML file of the child UserControl. Especially when considering what is said in this StackOverflow topic. When would you ever want to use the child's namescope from outside of the element's XAML file? I can understand why this is happening from a logical implementation perspective, but it doesn't feel like it makes much sense from a usability perspective.

Now, interestingly this issue has been reported for WPF too here. But I'm not fully satisfied by the accepted solution there because my understanding is that the namescope should not be applied outside of the control, but I might be missing something. And when I test this myself in C# with WPF, it all seems to work as I expect it to, I don't get this issue at all, both when running the WPF app and when viewing it in the designer in Blend. So I'm wondering if this is something that Microsoft may have patched to change the behavior of after the post was made, or if there's something else that's causing me to not run into the issue on the C# side...

This issue can of course be worked around by either changing the names to something more specific, or using RelativeSource instead of ElementName. But either way, this is not the behavior that I would expect, which is why I decided to make a post about it. :)
 
User avatar
sfernandez
Site Admin
Posts: 3029
Joined: 22 Dec 2011, 19:20

Re: Namescopes in nested UserControls

26 May 2023, 13:47

So I'm wondering if this is something that Microsoft may have patched
It seems so: https://referencesource.microsoft.com/# ... Ref.cs,181

Could you please report this issue in our bugtracker, we will see how to solve it.
In the meantime, as you mentioned, you can use a different name or use RelativeSource in your inner UserControl.
 
anton.sodergren_pdx
Topic Author
Posts: 18
Joined: 24 Mar 2022, 17:10

Re: Namescopes in nested UserControls

26 May 2023, 15:48

I see. I made a ticket for it now: https://www.noesisengine.com/bugs/view.php?id=2610

Cheers!
 
User avatar
sfernandez
Site Admin
Posts: 3029
Joined: 22 Dec 2011, 19:20

Re: Namescopes in nested UserControls

29 May 2023, 13:20

Thanks for the report!

Who is online

Users browsing this forum: Google [Bot] and 8 guests