Search code examples
c#wpfbindingcomboboxelementname

Combobox is showing bound values but selection isn't possible


I have some really strange behaviour with a combobox. Threre is a ObservabalCollection bound to it. Values are displayed correctly. I can add some values to my collection bound to combobox_PragmaSections, they are displayed and I can switch between them.

There are some textboxes and a DoubleUpDown where I can set some propertys of the selected item of combobox_PragmaSections.

When I insert some values in the Textbox or the DoubleUpDown the combobox_PragmaSections "freezes". It shows the containing values, dropdown is possible BUT if I try to select another value than the current, nothing happens. When I remove my isEqual and getHashCode functions it works.... does anyone have an Idea what is happening?

<ComboBox x:Name="combobox_PragmaSections" ItemsSource="{Binding currentTask.list_PragmaSections}"
DisplayMemberPath="ViewName" Margin="13,271,10,0" VerticalAlignment="Top"
Grid.Column="1"/>

<TextBox Text="{Binding SelectedItem.Name,ElementName=combobox_PragmaSections}" 
    Height="23" Margin="13,298,10,0" TextWrapping="Wrap" 
    VerticalAlignment="Top" Grid.Column="1"/>



<TextBox Text="{Binding SelectedItem.FLAGS, ElementName=combobox_PragmaSections}" Height="23" Margin="13,324,10,0" 
        TextWrapping="Wrap" VerticalAlignment="Top" Grid.Column="1"/>

<xctk:DoubleUpDown Value="{Binding SelectedItem.Alignment,
ElementName=combobox_PragmaSections}" Margin="96,353,10,0"
VerticalAlignment="Top" Grid.Column="1" Height="20"/>


    public override bool Equals(System.Object obj)
    {
        // If parameter is null return false.
        if (obj == null)
        {
            return false;
        }

        // If parameter cannot be cast to Point return false.
        Task_PragmaSection p = obj as Task_PragmaSection;
        if ((System.Object)p == null)
        {
            return false;
        }
        bool isEqual = false;
        // If parameter is null return false:
        if ((object)p == null)
        {
            return false;
        }
        if (this.Unit == p.Unit &&
           this.Size == p.Size &&
           this.FLAGS == p.FLAGS &&
           this.File_Path == p.File_Path &&
           this.Description == p.Description &&
           this.completeSection == p.completeSection &&
           this.Alignment == p.Alignment &&
           this.Name == p.Name &&
           this.ViewName == p.ViewName &&
           this.Type == p.Type &&
           this.Qualifier == p.Qualifier &&
           this.list_Objects == p.list_Objects)
        {
            isEqual = true;
        }
        // Return true if the fields match:
        return isEqual;
    }
    public override int GetHashCode()
    {
        unchecked
        {
            int hash = 486187739;
            if (this.Name!=null)
            {
                hash = hash * 23 + this.Name.GetHashCode();
            }
            if(this.ViewName!=null)
            {
                hash = hash * 23 + this.ViewName.GetHashCode();
            }
            if(this.Alignment!=null)
            {
                hash = hash * 23 + this.Alignment.GetHashCode();
            }
            return hash;
        }
    }

Solution

  • Your GetHashCode() looks incorrect since the value you implementation returns will change during the object's lifetime (Once Name, ViewName or Alignment are assigned). This means instances can get lost in dictionaries or hashsets. If you don't have to put your objects as keys in dictionary (and you almost always can find an alternative) you shouldn't override the GetHashCode().