Name registration error when setting ContentTemplate by code
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);
- Attachments
-
- NameScopeIssue.zip
- (6.21 KiB) Downloaded 277 times
-
-
sfernandez
Site Admin
- Posts: 3364
- Joined:
Re: Name registration error when setting ContentTemplate by code
This is happening because you are creating an instance of the UserControl (which loads all the content) and adding that to the template. Your solution of clearing the content is fine, but it would be better if you define the template using a xaml string to avoid the control instantiation:
Code: Select all
const char* xaml = R"(
<DataTemplate
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="namespace:NameScopeIssue">
<local:MyControl/>
</DataTemplate>)";
Ptr<DataTemplate> contentTemplate = StaticPtrCast<DataTemplate>(GUI::ParseXaml(xaml));Re: Name registration error when setting ContentTemplate by code
@sfernandez Yes that approach solves the issue and is applicable to our case. Thank you.
-
-
sfernandez
Site Admin
- Posts: 3364
- Joined:
Re: Name registration error when setting ContentTemplate by code
Good, thanks for the update, marking this as solved.
Who is online
Users browsing this forum: No registered users and 9 guests