-
- anton.sodergren_pdx
- Posts: 13
- Joined:
Namescopes in nested UserControls
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:
So far it's all fine.
Now for the parent UserControl:
Here I would expect the binding 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. :)
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:
Code: Select all
<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>
Now for the parent UserControl:
Code: Select all
<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>
Code: Select all
ChildControlName="{Binding ElementName=Root, Path=ParentControlName}"
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. :)
-
-
sfernandez
Site Admin
- Posts: 2866
- Joined:
Re: Namescopes in nested UserControls
It seems so: https://referencesource.microsoft.com/# ... Ref.cs,181So I'm wondering if this is something that Microsoft may have patched
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
- Posts: 13
- Joined:
-
-
sfernandez
Site Admin
- Posts: 2866
- Joined:
Re: Namescopes in nested UserControls
Thanks for the report!
Who is online
Users browsing this forum: Bing [Bot], ulisesBB and 4 guests