Name registration error when setting ContentTemplate by code
Posted: 04 Apr 2022, 10:53
I'm trying to switch the ContentTemplate of a ContentControl (e.g. Window or UserControl) in runtime. For example, I have a UserControl defined in XAML such as:
and then in my Window I use the following code to switch the Window's ContentTemplate:
The code seems to work but Noesis reports the following error:
I'm not sure if the error can be safely ignored. I tried similar code in WPF and there's no error. It seems that Noesis tries to register the name twice during template application:
Is the workaround safe, or is there a better way to achieve this?
Code: Select all
<UserControl x:Class="NameScopeIssue.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:NameScopeIssue"
mc:Ignorable="d">
<Grid>
<TextBlock x:Name="AAA" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center">AAA</TextBlock>
</Grid>
</UserControl>
Code: Select all
Symbol controlId("NameScopeIssue.MyControl");
auto content = Noesis::DynamicPtrCast<ContentControl>(Noesis::Factory::CreateComponent(controlId));
if (content)
{
auto contentTemplate = Noesis::MakePtr<DataTemplate>();
contentTemplate->SetVisualTree(content.GetPtr());
SetContentTemplate(contentTemplate);
}
Code: Select all
Cannot register duplicate name 'AAA' in this namescope.
- When the template's content is cloned and added as child to TemplatedParent
- When the template's content has a Content property, and the property is copied to the cloned element
Code: Select all
content->SetContent(Ptr<BaseComponent>()); // Avoid name registration error
auto contentTemplate = Noesis::MakePtr<DataTemplate>();
contentTemplate->SetVisualTree(content.GetPtr());
SetContentTemplate(contentTemplate);