Q:

C# WPF ComboBox. ObjectCollection. IndexOf Method (Object) can't find Object in ComboBox with an other object with the same value

I have a Combobox which contain IdDataClass object and im tying to find the index of an IdDataClass in my ComboBox with an IdDataClass with the same parameter as the one in the ComboBox.  
IdDataClass Instance1 = new IdDataClass(0, "Text");
IdDataClass Instance2 = new IdDataClass(0, "Text");
MyComboBox. Items. Add(Instance1);

int Index = MyComboBox. Items. IndexOf(Instance1);

This example will work and will return the index of ContactClientIdDataClass1
But this one
IdDataClass Instance1 = new IdDataClass(0, "Text");
IdDataClass Instance2 = new IdDataClass(0, "Text");
MyComboBox. Items. Add(Instance1);

int Index = MyComboBox. Items. IndexOf(Instance2);

will not work the value of index is -1.  My question is why can't i find the index of instance1 with instance2.  They have exactly the same value ?  
Ps.  I have try to overload the Equals function of my IdDataClass:
public class IdDataClass
{
     int _ID;
    string _MyData;

    public IdDataClass(int ID, string Data)
    {
        _ID = ID;
        _MyData = Data;
    }
    public bool Equals(IdDataClass p)
    {
        if ((object)p == null)
        {
            return false;
        }
        return (_ID == p._ID)&&(_MyData == p._MyData);
    }
}

A:

When you use IndexOf() the method uses Object. Equals() to determine whether two objects are alike.  So your own Equals method never gets called because it is not the same method.  You have to override the Equals method to obtain the behaviour you want.  Add the following lines:
public override bool Equals(object obj)
{
    IdDataClass otherObject = obj as IdDataClass;
    if (otherObject == null)
        return false;
    else
        return Equals(otherObject );
}

public override int GetHashCode()
{
    return _ID.GetHashCode() + _MyData. GetHashCode();
}

Overriding the GetHashCode() should be done as well as you never know how the items are stored within a collection.  It maybe uses some kind of HashTable for performance manner.  You should make clear that instances that are equal (according to your implementation) return the same hash code.