限制 Silverlight/WPF 行为可见性
在 Silverlight(也可能是 WPF)中,当我为例如 ItemsControl
定义 System.Windows.Interactivity.Behavior
时,就像
public class SomeAwesomaticBehavior : Behavior<ItemsControl>
{
}
它将出现在 Visual Studio 的 XAML 编辑器中 一样(也可能在设计器中)即使对于普通的非项目控件也会抛出令人讨厌的运行时异常。这与仅针对预期类型出现的附加属性相反。
有没有办法限制这种可见性?也许有一些神奇的属性(尽管这将是一个多余的声明)?
今天没有办法,希望以后有吗?因为当大量与当前对象无关的行为弹出时,它肯定会让同事和设计师感到困惑。
更新:我已提交用户语音项目。
In Silverlight (and probably WPF), when I define a System.Windows.Interactivity.Behavior<T>
for e.g. an ItemsControl
, like
public class SomeAwesomaticBehavior : Behavior<ItemsControl>
{
}
it will appear in Visual Studio's XAML editor (and probably in the Designer too) even for ordinary, non-Items-Controls and throw nasty runtime exceptions. This is contrary to Attached Properties which will appear only for intended types.
Is there a way to restrict that visibility? Some magic attribute maybe (although that would be a redundant declaration)?
If there is no way today, I hope there will be so in the future? Because it surely does confuse co-workers and designer folks when a lot of Behaviors pop up that don't have anything to do with the current object.
Update: I have filed uservoice items.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@HeRz你是对的,没有办法按目标类型过滤行为。 Blend(可能还有 vs 设计器)使用反射来查找您创建的从基本类型Behavior 继承的所有类型,并将它们显示在资源列表中。
Blend 将防止您将行为或触发器拖到不想要的项目上。因此这应该有助于防止它们的滥用。
我通常尝试将行为编写为可重用的代码片段,而不是局限于特定情况。它们只是具有特定用途的工具。
@HeRz you are correct, there is no way to filter behaviors by their targeted type. Blend (and probably vs designer) use reflection to find all of the types you create which inherit from base type Behavior and displays them in the assets list.
Blend will prevent you from dragging a behavior or trigger onto an item which it is not intended. So that should help prevent their misuse.
I usually try to write behaviors as reusable pieces of code, not scoped to a specific case. They are simply tools with specific purposes.