3

I'm writing some WPF custom controls for a third part library. For instance I enhanced the standard ComboBox with some dependency properties. The main problem is that my controls have as private instances some IDisposable objects, and I'd like to dispose these objects. The structure of my controls is something like this :

public class MyComboBox : ComboBox

{

private IDisposableObject _innerObject;   
[..]

} 

How can I ensure this object is disposed when the control is GC'ed and what is the best way to do that?

Thanks in advance.

P.S.: I tried with the Finalizer method, but I think that it's not a clean and good solution, and with the Unloaded event of the control (that it's raised also when the themes changes).

César
  • 9,206
  • 5
  • 46
  • 69

1 Answers1

0

Have your MyComboBox Implement IDisposable and in that dispose menthod dispose of your other objects. When you are done with your combo box call dispose on it.

public class MyComboBox : ComboBox, IDisposable 
{ 

    private IDisposableObject _innerObject;    
    [..] 

    public void  Dispose()
    {
        _innerObject.Dispose();
    }
}
Paul Matovich
  • 1,336
  • 14
  • 20
  • 1
    Hi thanks for your answer. The problem is that the ComboBox belongs to a control library, that tipically it's used only within XAML code. So the Dispose method is not called. – user1070316 Nov 29 '11 at 00:49