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>

</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>

</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.
<Expander VerticalAlignment=""Center"" xmlns:i=""http://schemas.microsoft.com/xaml/behaviors"">
and even it's declared with the othersxlmns
on top, the issue is happening too. – Saturniid