1

私はWPFで非常に面白い問題を抱えています

コードを使用してコンボボックスを作成し、それをコントロールに追加します。

Combobox.SelectedItem または Combobox.SelectedIndex または Combobox.SelectedValue を設定すると、コンボボックス項目から別のオプションを選択できません。

ForeignKeyDisplayAttribute attribute = (ForeignKeyDisplayAttribute)this.GetAttribute(typeof(ForeignKeyDisplayAttribute), property);  
if (attribute != null)  
{  
    ForeignKeyDisplayAttribute fkd = attribute;  
    Type subItemType = fkd.ForeignKeyObject;  
    contentControl = new ComboBox();  
    object blankItem = System.Activator.CreateInstance(subItemType, new object[] { });  
    System.Reflection.MethodInfo method = subItemType.GetMethod("Load", new Type[] { typeof(int) });  
    object innerValue = method.Invoke(null, new object[] { value });  
    System.Collections.IList selectedSubItems = (System.Collections.IList)subItemType.GetMethod("Load", Type.EmptyTypes).Invoke(null, new object[] { });  
    selectedSubItems.Insert(0, blankItem);  
    ((ComboBox)contentControl).SelectedValuePath = fkd.IdField;  
    ((ComboBox)contentControl).DisplayMemberPath = fkd.DescriptionField;  
    ((ComboBox)contentControl).ItemsSource = selectedSubItems;  
    ((ComboBox)contentControl).InvalidateVisual();  
    // If I use any of the two below lines or SelectedItem then I can't change the value via the UI.
    ((ComboBox)contentControl).SelectedIndex = this.FindIndex(selectedSubItems, value);  
    ((ComboBox)contentControl).SelectedValue = value;  
}  

これをどのように修正できるかについてのアイデアはありますか?

4

2 に答える 2

0

よく答えが見つかりました。

オーバーライドされたオブジェクトの Equals メソッドを間違ってコーディングしていたことが判明し、常に false を返していました。

    public override bool Equals(object obj)
    {
        if (obj is IId)
            return this.Id.Equals(((IId)obj).Id);
        return false;
    }  

になるはずだった

    public override bool Equals(object obj)
    {
        if (obj.GetType() == this.GetType() && obj is IId)
            return this.Id.Equals(((IId)obj).Id);
        return false;
    }
于 2010-08-10T12:08:19.800 に答える
0

GUI でそれらの ComboBox 項目にバインディングがありますか? 単純な理由は、コード ビハインドで手動で値を設定するとバインディングが破棄されるためです。

回避策: 値を手動で設定する前に、BindingOperations静的関数を使用してバインディングを取得できます。

Binding b = BindingOperations.GetBinding(yourComboBox, ComboBox.SelectedItemProperty);

// do your changes here

BindingOperations.SetBinding(yourComboBox, ComboBox.SelectedItemProperty, b);

1月

于 2010-08-10T12:09:08.937 に答える