Interaction triggers inside DataTemplate not working with XamlReader
Asked Answered
S

1

8

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.

Saturniid answered 22/11, 2021 at 22:56 Comment(4)
@Shrimperator : it's declared in <Expander VerticalAlignment=""Center"" xmlns:i=""http://schemas.microsoft.com/xaml/behaviors""> and even it's declared with the others xlmns on top, the issue is happening too.Saturniid
ah, my bad. I think the double quotation marks tripped me up :)Sumerian
I cannot reproduce the issue with .NET 4.7.2 and Microsoft.Xaml.Behaviors.Wpf 1.1.39Bacteroid
@Bacteroid : still the same with targeted framework 4.7.2 and 4.8 with Microsoft.Xaml.Behaviors.Wpf 1.1.39Saturniid
B
6

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""
Bacteroid answered 26/11, 2021 at 11:25 Comment(2)
Amazing! So many hours spent trying to understand and comment in and out my behaviors in templates. The fully qualified namespace is magical! Thank you so much! @BacteroidAcephalous
Thank you so much for this answer. Elmish.WPF code Sample 'NewWindow' was giving me trouble because it uses 'InteractionTriggers'. Looking around there are lots of suggested NuGet packages. Only Microsoft.Xaml.Behaviors.Wpf.dll provided the functionality. Again thanks!Costanza

© 2022 - 2024 — McMap. All rights reserved.