DataTemplate 内部的交互触发器不适用于 XamlReader
Interaction triggers inside DataTemplate not working with XamlReader
我正在尝试使用 XamlReader.Load()
解析在代码隐藏中动态创建的 DataTemplate
(对于 WPF 数据网格):
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;
当我删除这部分代码时一切正常:
<i:Interaction.Triggers>
<i:EventTrigger EventName=""IsExpanded"">
<i:InvokeCommandAction Command=""{{Binding DataContext.TextFilterColumn}}""/>
</i:EventTrigger>
</i:Interaction.Triggers>
但是当我添加它时,有一个 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'.'
我在本文之后使用“XAML 行为”(但 Interactivity
也是如此):
https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/
这似乎是 XamlReader.Load(xmlReader)
的问题。
如果有人知道解决方法,我将不胜感激。
配置:
- 框架 4.8(试用 4.7.2)
- Microsoft.Xaml.Behaviors.Wpf
1.1.39
谢谢。
XamlReader.Load 的文档中未提及,但 XAML 命名空间映射中引用的任何自定义程序集必须已可供应用程序使用。
你有两个选择:
1.Load 程序集 Microsoft.Xaml.Behaviors
或在读取 xaml 输入之前从程序集中初始化某些类型。
Assembly assembly = Assembly.LoadFrom("Microsoft.Xaml.Behaviors.dll");
或
var et = new Microsoft.Xaml.Behaviors.EventTrigger();
2.Use xaml
中的 CLR 命名空间声明
xmlns:i=""clr-namespace:Microsoft.Xaml.Behaviors;assembly=Microsoft.Xaml.Behaviors""
我正在尝试使用 XamlReader.Load()
解析在代码隐藏中动态创建的 DataTemplate
(对于 WPF 数据网格):
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;
当我删除这部分代码时一切正常:
<i:Interaction.Triggers>
<i:EventTrigger EventName=""IsExpanded"">
<i:InvokeCommandAction Command=""{{Binding DataContext.TextFilterColumn}}""/>
</i:EventTrigger>
</i:Interaction.Triggers>
但是当我添加它时,有一个 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'.'
我在本文之后使用“XAML 行为”(但 Interactivity
也是如此):
https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/
这似乎是 XamlReader.Load(xmlReader)
的问题。
如果有人知道解决方法,我将不胜感激。
配置:
- 框架 4.8(试用 4.7.2)
- Microsoft.Xaml.Behaviors.Wpf 1.1.39
谢谢。
XamlReader.Load 的文档中未提及,但 XAML 命名空间映射中引用的任何自定义程序集必须已可供应用程序使用。
你有两个选择:
1.Load 程序集 Microsoft.Xaml.Behaviors
或在读取 xaml 输入之前从程序集中初始化某些类型。
Assembly assembly = Assembly.LoadFrom("Microsoft.Xaml.Behaviors.dll");
或
var et = new Microsoft.Xaml.Behaviors.EventTrigger();
2.Use xaml
中的 CLR 命名空间声明xmlns:i=""clr-namespace:Microsoft.Xaml.Behaviors;assembly=Microsoft.Xaml.Behaviors""