5

UIElement在 Silverlight 3.0 中,我在 Code Behind 中添加了自定义行为。

我想稍后在运行时删除行为。

将已添加的 Behavior 从 中分离的 C# 语法是UIElement什么?

4

1 回答 1

8

我猜你正在谈论从Behavior<T>Blend SDK 中的类派生的行为......

您是否仍然参考附加时的行为?

MyCustomBehavior myBehavior = new MyCustomBehavior();
myBehavior.Attach(myElement);
...
myBehavior.Detach();

编辑

如果在要分离行为实例时不再引用它,则可以执行以下操作来分离 DependencyObject 上的所有行为:

foreach (var behavior in Interaction.GetBehaviors(myElement))
{
    behavior.Detach();
}
于 2010-02-25T03:16:09.210 回答