Search code examples
wpfcontroltemplatenullreferenceexceptionlistviewitem

"Object reference not set to an instance of an object." when setting a listview template


I'm trying to switch between 2 templates on the basis of a data trigger. But I app crashes with the "Object reference not set to an instance of an object." If I comment out the setter for the template it runs fine.

Here's my XAML:

<Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="Template" Value="{StaticResource Default}"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=FlagVar}" Value="true">
                <Setter Property="Template" Value="Error"/>
                <Setter Property="Background" Value="{DynamicResource ListErrorBackgroundColor}"/>
                <Setter Property="IsSelected" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
</Style>

EDIT- The above styling is in a ResourceDictionary which is merged into the consuming XAML.


Solution

  • What is the Value="Error" means? you can't set the Template like that, check this article

    http://msdn.microsoft.com/en-us/library/ms788717(VS.85).aspx