限制 Silverlight/WPF 行为可见性

发布于 2024-09-18 12:45:05 字数 1083 浏览 7 评论 0原文

在 Silverlight(也可能是 WPF)中,当我为例如 ItemsControl 定义 System.Windows.Interactivity.Behavior 时,就像

public class SomeAwesomaticBehavior : Behavior<ItemsControl>
{
}

它将出现在 Visual Studio 的 XAML 编辑器中 一样(也可能在设计器中)即使对于普通的非项目控件也会抛出令​​人讨厌的运行时异常。这与仅针对预期类型出现的附加属性相反。

有没有办法限制这种可见性?也许有一些神奇的属性(尽管这将是一个多余的声明)?

今天没有办法,希望以后有吗?因为当大量与当前对象无关的行为弹出时,它肯定会让同事和设计师感到困惑。

更新:我已提交用户语音项目。

Silverlight: http: //dotnet.uservoice.com/forums/4325-silverlight-feature-suggestions/suggestions/1224253-restrict-behavior-visibility?ref=title

WPF:http://dotnet.uservoice.com/forums/40583-wpf-feature -suggestions/suggestions/1224259-restrict-behavior-visibility?ref=title

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.

Silverlight: http://dotnet.uservoice.com/forums/4325-silverlight-feature-suggestions/suggestions/1224253-restrict-behavior-visibility?ref=title

WPF: http://dotnet.uservoice.com/forums/40583-wpf-feature-suggestions/suggestions/1224259-restrict-behavior-visibility?ref=title

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2024-09-25 12:45:05

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文