相关疑难解决方法(0)

如何修复AttachedBehavior上的DependencyPropertyDescriptor AddValueChanged内存泄漏?

我知道我需要调用RemoveValueChanged,但我找不到一个可靠的地方来调用它.我知道可能没有一个.

我看起来需要找到一种不同的方法来监控更改,然后使用AddValueChanged添加一个处理程序.我正在寻找有关实现这一目标的最佳方法的建议.我已经看到了在PropertyMetadata中使用PropertyChangedCallback的建议,但是当我的TextBox和Adorner不是静态的时候我不确定如何做到这一点.此外,IsFocused属性不是在我的类中创建的DependencyProperty.

谢谢.

public sealed class WatermarkTextBoxBehavior
{
    private readonly TextBox m_TextBox;
    private TextBlockAdorner m_TextBlockAdorner;

    private WatermarkTextBoxBehavior(TextBox textBox)
    {
        if (textBox == null)
            throw new ArgumentNullException("textBox");

        m_TextBox = textBox;
    }

    #region Behavior Internals

    private static WatermarkTextBoxBehavior GetWatermarkTextBoxBehavior(DependencyObject obj)
    {
        return (WatermarkTextBoxBehavior)obj.GetValue(WatermarkTextBoxBehaviorProperty);
    }

    private static void SetWatermarkTextBoxBehavior(DependencyObject obj, WatermarkTextBoxBehavior value)
    {
        obj.SetValue(WatermarkTextBoxBehaviorProperty, value);
    }

    private static readonly DependencyProperty WatermarkTextBoxBehaviorProperty =
        DependencyProperty.RegisterAttached("WatermarkTextBoxBehavior",
            typeof(WatermarkTextBoxBehavior), typeof(WatermarkTextBoxBehavior), new UIPropertyMetadata(null));

    public static bool GetEnableWatermark(TextBox obj)
    {
        return (bool)obj.GetValue(EnableWatermarkProperty);
    }

    public static void SetEnableWatermark(TextBox obj, bool value) …
Run Code Online (Sandbox Code Playgroud)

wpf memory-leaks dependency-properties attachedbehaviors

9
推荐指数
2
解决办法
7336
查看次数