【问题标题】:How to access the behavior that was attached to a UI element?如何访问附加到 UI 元素的行为?
【发布时间】:2011-07-07 12:19:34
【问题描述】:

例如,我有这个:

Border myBorder = new Border();
...
designArea.Children.Add(myBorder);

ResizeAndMoveBehavior b = new ResizeAndMoveBehavior();
b.CurrentProperties = thisButtonProperties;
b.Attach(myBorder);

现在,当我从 designArea 的子项中获取 myBorder 时,如何访问附加到 myBorder 的行为的 CurrentProperties?

【问题讨论】:

    标签: silverlight dependency-properties behavior


    【解决方案1】:

    您以非标准方式附加您的行为,这意味着以后无法恢复附加的行为。但是,如果您以标准方式执行此操作,则可以获得此信息。以下是应如何管理行为的示例。

    using System.Windows.Interactivity;
    
    // Add a behavior.
    Interaction.GetBehaviors(myBorder).Add(b);
    
    // Get all behaviors of an object.
    BehaviorCollection behaviors = Interaction.GetBehaviors(myBorder);
    
    // Get a specific type of behavior.
    ResizeAndMoveBehavior myBehavior = (ResizeAndMoveBehavior)behaviors
        .Where(b => b is ResizeAndMoveBehavior).Single();
    

    【讨论】:

    • 我只是一个初学者,所以你的回答很有帮助。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多