How to use DataTemplateSelector?
Posted: 13 Mar 2015, 16:13
I'm trying to port from a WPF application that is using a DataTemplateSelector.
The XAML is defining DataTemplate resources, a DataTemplateSelector, and assigning DataTemplates to the selector's properties.
I have created a class to implement my DataTemplateSelector
While defining my DataTemplate properties,
I followed the pattern I had used elsewhere for defining a property of type NsBool, but this is not working.
When I compile I get:
error C2248: 'Noesis::Gui::FrameworkTemplate::operator =' : cannot access private member declared in class 'Noesis::Gui::FrameworkTemplate'
Thinking that this error was caused by the assignments done in the Setters, I commented out the assignments.
Then I get:
error C2248: 'Noesis::Core::BaseComponent::BaseComponent' : cannot access private member declared in class 'Noesis::Core::BaseComponent'
This diagnostic occurred in the compiler generated function 'Noesis::Gui::FrameworkTemplate::FrameworkTemplate(const Noesis::Gui::FrameworkTemplate &)'
Should I be attempting to define properties of type DataTemplate?
Is it possible to set their values in the xaml as I am attempting to do?
How would this be accomplished?
The XAML is defining DataTemplate resources, a DataTemplateSelector, and assigning DataTemplates to the selector's properties.
Code: Select all
<DataTemplate x:Key="TextDataTemplate">
<TextBlock TextWrapping="NoWrap" Text="{Binding StatusItemText}" Margin="5,0,0,0" Focusable="False" FontFamily="#/Fonts/Arial" FontSize="16" Foreground="#EBEBEB"/>
</DataTemplate>
<DataTemplate x:Key="IconDataTemplate">
<Grid Height="24" Width="24" Margin="5,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False">
<ContentControl Content="{Binding StatusItemIconPath}" Focusable="False"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="BigIconDataTemplate">
<Grid Height="32" Width="32" Margin="5,0,5,0" HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False">
<ContentControl Content="{Binding StatusItemIconPath}" Focusable="False"/>
</Grid>
</DataTemplate>
<local:StatusItemTemplateSelector x:Key="statusItemTemplateSelector"
TextDataTemplate="{StaticResource TextDataTemplate}"
IconDataTemplate="{StaticResource IconDataTemplate}"
BigIconDataTemplate="{StaticResource BigIconDataTemplate}"/>
Code: Select all
NS_DECLARE_SYMBOL(TextDataTemplate)
NS_DECLARE_SYMBOL(IconDataTemplate)
NS_DECLARE_SYMBOL(BigIconDataTemplate)
class StatusItemTemplateSelector : public DataTemplateSelector
{
public:
StatusItemTemplateSelector() {}
~StatusItemTemplateSelector() {}
DataTemplate* SelectTemplate(Core::BaseComponent* item, DependencyObject* container)
{
StatusBar_Item* statusItem = NsStaticCast<StatusBar_Item*>(item);
if (statusItem == nullptr)
return &_textDataTemplate;
NsString idString = statusItem->GetStatusItemID();
if (idString == "A")
return &_iconDataTemplate;
else if (idString == "B")
return &_bigIconDataTemplate;
else
return &_textDataTemplate;
}
DataTemplate GetTextDataTemplate() const
{
return _textDataTemplate;
}
void SetTextDataTemplate(const DataTemplate pTemplate)
{
_textDataTemplate = pTemplate;
}
DataTemplate GetIconDataTemplate() const
{
return _iconDataTemplate;
}
void SetIconDataTemplate(const DataTemplate pTemplate)
{
_iconDataTemplate = pTemplate;
}
DataTemplate GetBigIconDataTemplate() const
{
return _bigIconDataTemplate;
}
void SetBigIconDataTemplate(const DataTemplate pTemplate)
{
_bigIconDataTemplate = pTemplate;
}
private:
DataTemplate _textDataTemplate;
DataTemplate _iconDataTemplate;
DataTemplate _bigIconDataTemplate;
NS_IMPLEMENT_INLINE_REFLECTION(StatusItemTemplateSelector, DataTemplateSelector)
{
NsMeta<TypeId>("StatusItemTemplateSelector");
NsProp("TextDataTemplate", &StatusItemTemplateSelector::GetTextDataTemplate, &StatusItemTemplateSelector::SetTextDataTemplate);
NsProp("IconDataTemplate", &StatusItemTemplateSelector::GetIconDataTemplate, &StatusItemTemplateSelector::SetIconDataTemplate);
NsProp("BigIconDataTemplate", &StatusItemTemplateSelector::GetBigIconDataTemplate, &StatusItemTemplateSelector::SetBigIconDataTemplate);
}
};
I followed the pattern I had used elsewhere for defining a property of type NsBool, but this is not working.
When I compile I get:
error C2248: 'Noesis::Gui::FrameworkTemplate::operator =' : cannot access private member declared in class 'Noesis::Gui::FrameworkTemplate'
Thinking that this error was caused by the assignments done in the Setters, I commented out the assignments.
Then I get:
error C2248: 'Noesis::Core::BaseComponent::BaseComponent' : cannot access private member declared in class 'Noesis::Core::BaseComponent'
This diagnostic occurred in the compiler generated function 'Noesis::Gui::FrameworkTemplate::FrameworkTemplate(const Noesis::Gui::FrameworkTemplate &)'
Should I be attempting to define properties of type DataTemplate?
Is it possible to set their values in the xaml as I am attempting to do?
How would this be accomplished?