Search code examples
c#wpfeventtriggerxamlreader

Interaction triggers inside DataTemplate not working with XamlReader


I'm trying to parse with XamlReader.Load() a DataTemplate (for a WPF datagrid) created dynamically in code behind :

DataTemplate dataTemplate;

StringReader template = new StringReader($@"
<DataTemplate
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
    xmlns:local=""clr-namespace:MyApp;assembly=MyApp"">
<DataTemplate.Resources>
    <local:ArrayMultiValueConverter x:Key=""arrayMultiValueConverter""/>
</DataTemplate.Resources>
    <StackPanel Orientation=""Vertical"">
        <Expander VerticalAlignment=""Center"" xmlns:i=""http://schemas.microsoft.com/xaml/behaviors"">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName=""IsExpanded"">
                <i:InvokeCommandAction Command=""{{Binding DataContext.TextFilterColumn}}""/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
            <Expander.Header>
                <TextBlock x:Name=""{dtColumnName}"" VerticalAlignment=""Center"" Text=""{{TemplateBinding Content}}"" Margin=""5,5,5,0"" FontWeight=""SemiBold""/>
            </Expander.Header>
            <StackPanel Orientation=""Horizontal"">
                <TextBox x:Name=""{"TbxFilter" + dtColumnName}"" Width=""100"" Margin=""5""/>
                <TextBlock Margin=""0,0,5,0"" VerticalAlignment=""Center"" FontFamily=""Segoe MDL2 Assets"">
                    <Hyperlink TextDecorations=""{{x:Null}}"" Command=""{{Binding DataContext.FilterColumnName, RelativeSource={{RelativeSource FindAncestor, AncestorType={{x:Type Window}}}}}}"">
                        <Hyperlink.CommandParameter>
                            <MultiBinding Converter=""{{StaticResource arrayMultiValueConverter}}"">
                                <Binding Path=""Text"" ElementName=""{dtColumnName}"" />
                                <Binding Path=""Text"" ElementName=""{"TbxFilter" + dtColumnName}"" />
                            </MultiBinding>
                        </Hyperlink.CommandParameter>   
                        &#xE721;
                    </Hyperlink>
                </TextBlock>
                <TextBlock Margin=""0,0,5,0"" VerticalAlignment=""Center"" FontFamily=""Segoe MDL2 Assets"">
                    <Hyperlink TextDecorations=""{{x:Null}}"" Command=""{{Binding DataContext.FilterColumnName, RelativeSource={{RelativeSource FindAncestor, AncestorType={{x:Type Window}}}}}}"">
                        <Hyperlink.CommandParameter>
                            <MultiBinding Converter=""{{StaticResource arrayMultiValueConverter}}"">
                                <Binding Path=""Text"" ElementName=""{dtColumnName}""/>
                                <Binding Path=""Text"" RelativeSource=""{{RelativeSource FindAncestor, AncestorType={{x:Type TextBlock}}}}""/>
                            </MultiBinding>
                        </Hyperlink.CommandParameter>
                        &#xe75c;
                    </Hyperlink>
                </TextBlock>
            </StackPanel>
        </Expander>
    </StackPanel>
</DataTemplate>
");

XmlReader xmlReader = XmlReader.Create(template);
dataTemplate = XamlReader.Load(xmlReader) as DataTemplate;

textColumn.HeaderTemplate = dataTemplate;

All is working when I remove this part of code :

<i:Interaction.Triggers>
    <i:EventTrigger EventName=""IsExpanded"">
        <i:InvokeCommandAction Command=""{{Binding DataContext.TextFilterColumn}}""/>
    </i:EventTrigger>
</i:Interaction.Triggers>

But when I add it, there is an Exception Thrown :

System.Windows.Markup.XamlParseException: ''Cannot set unknown member '{http://schemas.microsoft.com/xaml/behaviors}Interaction.Triggers'.' Line number '11' and line position '10'.'

I use "XAML Behaviors" following this article (but the same happened with Interactivity) :

https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/

It seems to be an issue with XamlReader.Load(xmlReader).

If someone knows a workaround, I will be grateful.

Configuration :

  • Framework 4.8 (tried with 4.7.2)
  • Microsoft.Xaml.Behaviors.Wpf 1.1.39

Thanks.


Solution

  • It's not mentioned in the doucmentation of XamlReader.Load but any custom assemblies referenced in a XAML namespace mapping must already be available to the application.

    You have two options:

    1.Load assembly Microsoft.Xaml.Behaviors or initialize some type from the assembly before reading xaml input.

    Assembly assembly = Assembly.LoadFrom("Microsoft.Xaml.Behaviors.dll");
    

    or

    var et = new Microsoft.Xaml.Behaviors.EventTrigger();
    

    2.Use the CLR namespace declaration in xaml

    xmlns:i=""clr-namespace:Microsoft.Xaml.Behaviors;assembly=Microsoft.Xaml.Behaviors""