36

I have a tree view that I'm binding to with some custom viewmodels. The viewmodels are in an ObservableCollection and inherit ViewModelBase which inherits INotifyPropertyChanged.

It compiles and run fine, but in the designer I'm getting the error:

"DataTemplate.DataType cannot be type object

Parameter name: value"

My XAML is:

<TreeView Grid.Row="1" ItemsSource="{Binding ResultsTree}" SelectedItemChanged="TreeView_OnSelectedItemChanged">
<TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type local:TreeViewItemViewModel}" ItemsSource="{Binding Path=Children}">
        <StackPanel Orientation="Horizontal">
            <CheckBox IsChecked="{Binding IsChecked}"/>
            <TextBlock Text="{Binding Text}"/>
        </StackPanel>
    </HierarchicalDataTemplate>
    
    
    <DataTemplate DataType="{x:Type local:CorrectionAndFreqViewModel}">
        <StackPanel Orientation="Horizontal" ToolTip="{Binding AmbientText}">
            <Rectangle Width="20" Height="5" Fill="{Binding LineColor, Converter={StaticResource ColorToSolidColorBrushValueConverter}}"></Rectangle>
            <CheckBox IsChecked="{Binding IsChecked}"/>
            <TextBlock Text="{Binding Text}"/>
        </StackPanel>
    </DataTemplate>
</TreeView.Resources>
</TreeView>

The properties window says its an Object too, but I have no idea why:

enter image description here

Any ideas?

5
  • 3
    Did you ever find a solution to this? I've run across the same problem and am stumped. It doesn't affect functionality but it sure affects my productivity as I sit here trying to figure out why I'm getting an error on a successful build. LOL Sep 13, 2018 at 4:23
  • @RickRiensche Unfortunately not, I'm just dealing with not being able to use the designer properly :(
    – Loocid
    Sep 13, 2018 at 4:50
  • i have run your code in visual studio 2019 community version 16.3.6 it work just fine ...
    – OMR
    Oct 29, 2019 at 15:53
  • 2
    This bug seems to still exist in visual studio 2019 community version 16.4.0 :(
    – Alan Wayne
    Nov 3, 2019 at 21:59
  • I did not have any of the problems you mentioned in the VS2019 WPF project (.NET Framework).Is your WPF project.NET Core?
    – i code
    Dec 17, 2020 at 1:31

6 Answers 6

3

I was able to solve this WPF Designer Bug by adding a x:Key to the DataTemplate

<ResourceDictionary>
    <DataTemplate x:Key="ThisKeySolvesDesignersNullRef" 
                  DataType="viewmodels:MyViewModel">
        <views:MyView/>
    </DataTemplate>
</ResourceDictionary> 

I had been searching for months, because it eventually appeared on different projects and sometimes appeared and disappeard without an explicit reason. Thanks to the helping answer on SO

2

I think it wants you to use an interface type instead of a class type.

So, if you define an interface ICorrectionAndFreqViewModel that exposes all of the properties that you'll be using for data binding and then have CorrectionAndFreqViewModel implement that interface, you should be good.

Like you said, the code will still compile and run without using an interface here. My guess as to why the designer complains about this is that data binding can only be used on public properties, so by using an interface, there's a guarantee that all the properties will be public.

1

You should have an xmlns tag like local, l, or some such. In the datatype you need to use local:CorrectionAndFreqViewModel rather than {x:Type CorrectionAndFreqViewModel}. If you don't have one of these xmlns values then you need to define it so that it points to the dll that you're using (or use a different marker if you're looking at another project e.g. xmlns:msl="clr-namespace:MediaSystems")

1
  • Writing the type "local:CorrectionAndFreqViewModel" without x:Type did the trick for me. Dec 4, 2020 at 12:34
1

I was able to fix this by making sure the file name is exactly as the class name.

0

I had the same problem. To fix do the following:

  1. Build and rebuild the project successfully.
  2. Go to Tool > Option > XAML > Uncheck Automaticaly name interactive....
  3. Restart Visual Studio, and then the problem will go away.
-2

Instead of 'object' set the 'DataType' to your actual view model type (e.g CorrectionAndFreqViewModel in your case) in properties.

Hope this will fix the issue.

Please mark it as answer if satisfies.

Ideally, it should automatically pick correct data type in properties, in my case it works correct.

1
  • 3
    It doesnt let me. Entering CorrectionAndFreqViewModel tells me it cant parse a string. Entering {x:type CorrectionAndFreqViewModel} just reverts back to Object.
    – Loocid
    Aug 2, 2018 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.