Combobox显示绑定值,但无法进行选择

时间:2015-02-04 20:22:10

标签: c# wpf binding combobox elementname

我在组合框中有一些非常奇怪的行为。 Threre是一个与它绑定的ObservabalCollection。值正确显示。我可以在绑定到combobox_PragmaSections的集合中添加一些值,它们会显示出来,我可以在它们之间切换。

有一些文本框和一个DoubleUpDown,我可以在其中设置所选项目的combobox_PragmaSections的一些属性。

当我在Textbox或DoubleUpDown中插入一些值时,combobox_PragmaSections会“冻结”。它显示包含值,下拉列表是可能的但是如果我尝试选择另一个值而不是当前值,则没有任何反应。当我删除我的isEqual和getHashCode函数时,它可以工作....有没有人知道发生了什么?

<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;
        }
    }

1 个答案:

答案 0 :(得分:1)

您的GetHashCode()看起来不正确,因为您实现返回的值将在对象的生命周期内更改(一旦分配了Name,ViewName或Alignment)。这意味着实例可能会在字典或散列集中丢失。如果您不必将对象作为键放在字典中(并且几乎总能找到替代方法),则不应该覆盖GetHashCode()。

相关问题