通过Style设置隐藏的AttachedProperty
我在使用 System.Windows.Interactivity.Interaction 附加行为类(来自 Expression Blend SDK 4)时遇到问题。我想在 XAML 样式元素中为 System.Windows.Window 类定义一对触发器。但由于 System.Windows.Interactivity.Interaction 类的 TriggersProperty 字段是私有的,并且此类中没有 SetTriggers 方法,因此我已经出现错误'设置属性 System.Windows.Setter.Property 引发异常。 ->值不能为空。运行以下代码时的参数名称:property'。
我真的很想在样式中使用触发器和操作,因为我想将它们用于我的窗口后代控件。当然,我可以使用我的自定义行为或直接使用触发器模拟逻辑对我的窗口后代类进行编码,但我想使用表达式库和我自己的现有触发器和操作,而不是拒绝它们,仅仅是因为 < Interaction类的em>TriggersProperty是隐藏的,我无法通过样式设置它。
有解决该问题的方法吗?通过反射还是其他方式?
附言。我已经尝试声明带有 TriggersProperty 附加依赖属性的自定义静态类,并在 AddOwner 方法的帮助下注册,但没有帮助 - 最后它仍然尝试访问相同的TriggersProperty 在同一个 System.Windows.Interactivity.Interaction 类中。
<Window
x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:windowsInteractivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<Window.Style>
<Style TargetType="Window">
<Setter Property="Title" Value="WindowStyleTest"/>
<Setter Property="windowsInteractivity:Interaction.Triggers">
<Setter.Value>
<windowsInteractivity:EventTrigger EventName="MouseDown"/>
</Setter.Value>
</Setter>
</Style>
</Window.Style>
</Window>
I've got a problem using System.Windows.Interactivity.Interaction attached behavior class (from Expression Blend SDK 4). I'd like to define a pair of triggers for System.Windows.Window class in XAML Style element. But as the TriggersProperty field of System.Windows.Interactivity.Interaction class is private and there is no SetTriggers method in this class, I've got an error 'Set property System.Windows.Setter.Property threw an exception. -> Value cannot be null. Parameter name: property' when running the following code.
I really want to use the triggers and actions in styles, because I'd like to use them for my window-descendant control. Of course I can use my custom behavior or directly code my window-descendant class with triggers-analogue logic, but I'd like to use already existent triggers and actions of the expression library and my own, not declining them, simply because the TriggersProperty of Interaction class is hidden and I can't set it through style.
Is any workaround for the problem? With Reflection or someway other?
PS. I already tried to declare custom static class with TriggersProperty attached dependency property, registered with the help of AddOwner method, but no help - at the end it still tries to access the same TriggersProperty in the same System.Windows.Interactivity.Interaction class.
<Window
x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:windowsInteractivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<Window.Style>
<Style TargetType="Window">
<Setter Property="Title" Value="WindowStyleTest"/>
<Setter Property="windowsInteractivity:Interaction.Triggers">
<Setter.Value>
<windowsInteractivity:EventTrigger EventName="MouseDown"/>
</Setter.Value>
</Setter>
</Style>
</Window.Style>
</Window>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
!!!更新!!!
好吧,我更进一步了。我扩展了该扩展以执行所有工作,包括设置触发器集合。
触发集合扩展
扩展可以完成所有繁重的工作。
注意:第一次调用 ProvideValue 时会加载样式,因此 TargetValue 是一个 Setter。
互动
TriggersCollection 的重新所有权/公开。
命令操作
一个自定义 TriggerAction,用于在 DataContext 上查找命令。
哇,老读者第一次发布代码。我终于明白了为什么代码的剪切和粘贴效果并不总是那么好。经过多次尝试才提交此更新。
我确信有磁盘空间、解析或渲染速度等原因,并且编辑器在提交失败时保持状态良好。
!!!Update!!!
Okay I took it a bit further. I extended the extension to perform all the work including setting the Triggers collection.
TriggerCollectionExtension
The Extension That does all the heavy lifting.
Note: The first time ProvideValue is called it will be from loading the style so the TargetValue is a Setter.
Interaction
The re-ownership/exposure of the TriggersCollection.
CommandAction
A custom TriggerAction that looks up the Command on the DataContext.
Wow long time reader first time posting code. I finally learned why the code doesn't always cut and paste so well. It took so many tries to submit this update.
I am sure there are reasons like disk space, parsing, or rendering speed, and the editor maintains state on failure to submit excellently.
我明白了,为什么会出现这个错误。这是因为在运行时它通过字符串名称搜索附加依赖项属性,即“ShadowTriggers”(如 System.Windows.Interactivity 程序集 Interaction 静态构造函数中指定的那样)。因此,我创建了自己的自定义静态类,并从 System.Windows.Interaction 继承了触发器依赖属性(通过 Reflection 和 AddOwner,只是将该属性公开为 ShadowTriggersProperty)。成功了!但是...现在我必须向样式的属性值设置器提供一个 TriggerCollection 实例,并且该类的构造函数是内部的。假设已经没有路可走了。
I got it, why the error appears. That's because at runtime it's searching an Attached Dependency property by string name, that is "ShadowTriggers" (as it specified in System.Windows.Interactivity assembly, Interaction static constructor). So I created the own custom static class and inherit the Triggers Dependency Property from System.Windows.Interaction there (via Reflection and AddOwner, just exposed the property as ShadowTriggersProperty). It worked! But... Now I have to provide a TriggerCollection instance to the Style's Property Value Setter, and the constructor of the class is internal. Suppose it is a no way further.