33

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?

Community
  • 1
  • 1
Loocid
  • 5,001
  • 1
  • 18
  • 39
  • 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 – Rick Riensche Sep 13 '18 at 04:23
  • @RickRiensche Unfortunately not, I'm just dealing with not being able to use the designer properly :( – Loocid Sep 13 '18 at 04:50
  • i have run your code in visual studio 2019 community version 16.3.6 it work just fine ... – OMR Oct 29 '19 at 15:53
  • 2
    This bug seems to still exist in visual studio 2019 community version 16.4.0 :( – Alan Wayne Nov 03 '19 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 '20 at 01:31

3 Answers3

1

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.

0

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")

-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.

  • 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 02 '18 at 07:25