wpfxamlxamlparseexception

Using Interaction.Triggers in DataTemplate causing XamlParseException only in Designer


I have an ItemsSource with a bound List of objects:


    <ItemsControl ItemsSource="{Binding Sprites}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Rectangle Fill="Black"
                                    Width="{Binding Width}"
                                    Height="{Binding Height}"/>
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDown">
                            <cmd:EventToCommand Command="{Binding MouseDownOnSpriteCommand}"
                                                PassEventArgsToCommand="True"
                                                EventArgsConverter="{StaticResource EventToEventCommandConverter}"
                                                EventArgsConverterParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UIElement}, AncestorLevel=1}}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

Now as you can see i tried adding a DataTemplate for the bound Items, however this causes the Visual Studio Designer to throw me the following error message:

XamlParseException: Collection property 'System.Windows.Controls.Grid'.'Triggers' is null.  
StackTrace:
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)  
at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)  
at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)  
at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)  
at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)  
at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)  
at System.Windows.FrameworkElement.ApplyTemplate()  
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)  
at System.Windows.UIElement.Measure(Size availableSize)  
at System.Windows.Controls.Canvas.MeasureOverride(Size constraint)  
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)  
at System.Windows.UIElement.Measure(Size availableSize)  
at System.Windows.ContextLayoutManager.UpdateLayout()  
at System.Windows.UIElement.UpdateLayout()  

My Code however compiles perfectly fine and I can run and execute it without any Problems. The issue is that, as long as the DataTemplate Code is uncommented, the Designer only shows me the Error Message.

I know that the DataTemplate is causing the Problem, because when i hardcode the same Grid into the ItemsControl and then use Interactions.Triggers, I can bind the Event and no Error appears in the Designer. (However I can only bind to Commands in the MainViewModel and not to the actual Items List).

So far I have tried to move the Template into a seperate ResourceDictionary, yet this hasn't changed anything. Also I know that someone had the same problem before. However Im using Visual Studio 2019 and I have already updated to the newest Version and again the Error still appears.


Solution

  • So I haven't found a direct solution but a workaround. One thing to note is that I defined my Window's DataContext by using

    <Window.DataContext>
        <vm:MainViewModel/>
    </Window.DataContext>
    

    And I believe that somehow influenced the error.
    And after testing, I have found that if I move the assigment of my DataContext to code behind, the designer doesn't show the error anymore and works fine. So basically I used

    public MainWindow() {
        InitializeComponent();
        MainViewModel vm = new MainViewModel();
        DataContext = vm;
    }
    

    as constructor for my code behind.