well,i tried this and the memory profiler shows that the problem still not solved, actually i removed the I was creating a DependencyProperty Changed with a callback method using the following code:
public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
{
Binding b = new Binding(propertyName) { Source = element };
var prop = System.Windows.DependencyProperty.RegisterAttached(
"ListenAttached" + propertyName,
typeof(object),
typeof(UserControl),
new System.Windows.PropertyMetadata(callback));
element.SetBinding(prop, b);
}
well,i tried this and the memory profiler shows that the problem still not solved,
actually i removed the element.SetBinding(prop, b); and the memory leak still happen so i thisnk the cause of it is not the binding it self but this code:
var prop = System.Windows.DependencyProperty.RegisterAttached(
"ListenAttached" + propertyName,
typeof(object),
typeof(UserControl),
new System.Windows.PropertyMetadata(callback));
when i use null instead of callback the leak didnt happen. that means that the cause is the callback method not binding,is there any way to unregister it? or register it in another way so it doesnt cause the leak?
Thanks alot Phil Sandler it realy helps and work fine
i was trying to add visibilitychanged listener to my user control so i call the method as RegisterForNotification("Visibility", this,callback),that was causing the memory leak,
i used what you sugested like this:
public string ListenAttachedVisibility { get { return (string)GetValue(SelectedValueBindingProperty); } set { SetValue(SelectedValueBindingProperty, value); } }
public static readonly DependencyProperty ListenAttachedVisibilityProperty =
DependencyProperty.Register("ListenAttachedVisibility", typeof(object), typeof(UserControl),
new System.Windows.PropertyMetadata(null, new PropertyChangedCallback(OnVisibilityChanged)));
private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// call to instance method
((DualLOV)d).OnVisibilityChanged(e);
}
protected virtual void OnVisibilityChanged(DependencyPropertyChangedEventArgs e)
{
ClearValues(e.NewValue);
}
and then i made the binding: Binding b = new Binding("Visibility") { Source = this }; this.SetBinding(ListenAttachedVisibilityProperty, b);
and the visibilty changed event worked fine also the memory leak disappeared. thanks again.