Reputation: 6441
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:
Any ideas?
Upvotes: 36
Views: 3372
Reputation: 33
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
Upvotes: 3
Reputation: 19
I was able to fix this by making sure the file name is exactly as the class name.
Upvotes: 1
Reputation: 1
I had the same problem. To fix do the following:
Tool
> Option
> XAML
> Uncheck Automaticaly name interactive...
.Visual Studio
, and then the problem will go away.Upvotes: 0
Reputation:
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
.
Upvotes: 2
Reputation: 147
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"
)
Upvotes: 1
Reputation: 20
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.
Upvotes: -2