WPF ComboBox Item Template Not Found

0

I'm trying to create a combo box with a custom data template but there will be several combo boxes that use the same template so I would like to make it a resource. It wasn't working so I created a simple test project to test this and sure enough I get the same issue. Here is the XAML code:

<Window x:Class="TemplateTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TemplateTest"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type={x:Type local:ViewModel}}"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ResourceDictionary>
            <DataTemplate DataType="local:DataItem" x:Key="DtTest">
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <ComboBox ItemsSource="{Binding NameOptions}" 
                      SelectedItem="{Binding SelectedName}"
                      ItemTemplate="{StaticResource DtTest}"/>
        </StackPanel>
    </Grid>
</Window>

I can post the other code too if anyone really wants to see it but the DataItem class is just a class with a single 'Name' string property. The ViewModel class is just a list of DataItems for the option and a selected one to bind to.

When I start this application I get this exception:

System.Windows.Markup.XamlParseException
  HResult=0x80131501
  Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '19' and line position '14'.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at TemplateTest.MainWindow.InitializeComponent() in C:\Users\sfaus\source\repos\TemplateTest\TemplateTest\MainWindow.xaml:line 1

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
Exception: Cannot find resource named 'DtTest'. Resource names are case sensitive.

The resource is clearly there and spelled correctly but it shows it can't find it. Why not???

I have also tried the following:

  • Taking out the key so that it should be the default template for that type. This doesn't create the error but also doesn't apply the template so it's basically ignored.
  • Putting the template in the ComboBox directly. I've confirmed this works as expected, but as I said I have quite a few of these ComboBoxes in my app so it would be great to not have to create the template each and every time...

Anyone know why this isn't working? Surely there is a way to set the item template from a resource... right?

c#
wpf
combobox
datatemplate
asked on Stack Overflow Jan 5, 2021 by sfaust

1 Answer

0

Ok so after a bit further search and investigation I think I found the issue. It seems you can't specify a Key AND a data type or it freaks out. The error message is very misleading but that seems to be the issue. If I take out the data type designation it starts working. In order to keep my Intellisense I set the design time data context on the root object of the template, so here is the updated template:

<DataTemplate x:Key="DtTest">
    <TextBlock d:DataContext="{d:DesignInstance Type={x:Type local:DataItem}}" Text="{Binding Name}" />
</DataTemplate>

That still doesn't explain why ONLY the data type just doesn't work but at least this gets it working for me. If anyone has any further information on they why there I'd be happy to accept a more detailed answer than mine.

answered on Stack Overflow Jan 5, 2021 by sfaust

User contributions licensed under CC BY-SA 3.0