In Windows 10, Visual Studio 2017, and .NET Framework 4.5, we created DataTemplateSelectors similar to the following:
ContentProperty("DataTemplateTable")
public class TypeToDataTemplateSelector:DataTemplateSelector
{
publicTypeToDataTemplateDictionaryDataTemplateTable {get;set;}
public override DataTemplate SelectTemplate (object item, DependencyObject container)
{
// Obtaining DataTemplate from DataTemplateTable
}
}
public classTypeToDataTemplateDictionary:Dictionary<Type,DataTemplate>{}
The code below worked fine.
<local:TypeToDataTemplateSelector x:Key="TypeToDataTemplateSelector">
<local:TypeToDataTemplateDictionary>
<DataTemplate x:Key="{x:Type local:ViewModelA}"DataType="local:ViewModelB">
<TextBlock Text="ViewModelA"/>
</DataTemplate>
<DataTemplate x:Key="{x:Type local:ViewModelB}"DataType="local:ViewModelB">
<TextBlock Text="ViewModelB"/>
</DataTemplate>
<DataTemplate x:Key="{x:Type local:ViewModelC}"DataType="local:ViewModelC">
<TextBlock Text="ViewModelC"/>
</DataTemplate>
</local:TypeToDataTemplateDictionary>
</local:TypeToDataTemplateSelector>
However, there was a problem using StaticResource.
<DataTemplate x:Key="DataTemplateA"DataType="local:ViewModelA">
<TextBlock Text="ViewModelA"/>
</DataTemplate>
<DataTemplate x:Key="DataTemplateB"DataType="local:ViewModelB">
<TextBlock Text="ViewModelB"/>
</DataTemplate>
<DataTemplate x:Key="DataTemplateC"DataType="local:ViewModelC">
<TextBlock Text="ViewModelC"/>
</DataTemplate>
<local:TypeToDataTemplateSelector x:Key="TypeToDataTemplateSelector">
<local:TypeToDataTemplateDictionary>
<StaticResource x:Key="{x:Type local:ViewModelA}"ResourceKey="DataTemplateA"/>
<StaticResource x:Key="{x:Type local:ViewModelB}"ResourceKey="DataTemplateB"/>
<StaticResource x:Key="{x:Type local:ViewModelC}"ResourceKey="DataTemplateC"/>
</local:TypeToDataTemplateDictionary>
</local:TypeToDataTemplateSelector>
The build passes, but the XamlObjectWriterException: 'StaticResourceHolder' object does not have a key value at runtime.' Line number 'xx', line position 'xx'.
and exception occur.
Can we somehow avoid this and use StaticResource for Dictionary?
c# wpf
Doesn't it just mean "{x:Type local:ViewModelA}" can't be used as a key value?
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
915 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
611 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.