将现有对象绑定到WPF C#MVVM中的ComboBox

时间:2017-09-06 13:40:15

标签: c# wpf mvvm combobox

我正在尝试使用ComboBox绑定到CostCodes列表。传入一个名为SelectedRecord的记录进行编辑,我希望发生两件事。首先,我希望ComboBox选择已应用于CostCode的{​​{1}}。然后,我希望用户能够修改SelectedRecord,以便他们可以将CostCode分配给其他人。

我已成功将SelectedRecord绑定到ComboBox,但是当用户加载ObservableCollection<CostCode>时,已分配给Window的{​​{1}}是未在CostCode中自动选择。这是XAML:

SelectedRecord

我尝试过使用ComboBox<TextBox Grid.Row="3" Grid.Column="1" Text="{Binding SelectedRecord.Provider}"/> <TextBox Grid.Row="4" Grid.Column="1" Text="{Binding SelectedRecord.Number}"/> <ComboBox Grid.Row="5" Grid.Column="1" ItemsSource="{Binding AllCostCodes}" DisplayMemberPath="Text"> </ComboBox> ,但没有运气。虽然用户可以进行编辑,但当用户点击SelectedValue时,SelectedValuePath会正确显示,但不会显示已分配给CostCodes的{​​{1}}。我包含了几个ComboBox以显示我对这些内容的约束力,我实际上希望对CostCode属性执行相同的操作,但使用SelectedRecord代替。

编辑:TextBoxes

Text

ComboBox

CostCode.cs
public partial class CostCode { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public CostCode() { this.PhoneRecords = new HashSet<PhoneRecord>(); } public int Id { get; set; } public string Text { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<PhoneRecord> PhoneRecords { get; set; } } PhoneRecord.cs中选择

public partial class PhoneRecord { public int Id { get; set; } public Nullable<System.DateTime> DateGiven { get; set; } public string PersonName { get; set; } public Nullable<int> PhoneModelId { get; set; } public Nullable<bool> NMU { get; set; } public string IMEI { get; set; } public string Number { get; set; } public Nullable<int> CostCodeId { get; set; } public Nullable<System.DateTime> DateReturned { get; set; } public string WhyReturned { get; set; } public string Solution { get; set; } public string Internet { get; set; } public string Provider { get; set; } public string SpeedDial { get; set; } public string OnDatabases { get; set; } public string Notes { get; set; } public virtual CostCode CostCode { get; set; } public virtual PhoneModel PhoneModel { get; set; } } ,然后传递到SelectedRecord,我想在其中设置适用的DataGrid

1 个答案:

答案 0 :(得分:0)

我认为您需要使用 SelectedItem 像这样:

<ComboBox Grid.Row="5" Grid.Column="1" 
            ItemsSource="{Binding AllCostCodes}"
            DisplayMemberPath="Text"
            SelectedItem="{Binding SelectedRecord.CostCode}">
</ComboBox>

修改 正如Spongebrot和Ed Plunkett在评论中所说:如果它不是CodeCode对象的同一个实例,那么你需要覆盖CostCode对象的Equals方法。
(在此编辑中,我还根据你问题中的评论,将#34; SelectedRecord&#34;和#34; SelectedRecord.CostCode&#34;的绑定链接起来。)

编辑2: 您可以在MSDN阅读有关覆盖等于的等号 在你的情况下,我认为它应该是这样的:

class CostCode
{
    //... property definition of Id and Text


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

        // If parameter cannot be cast to CostCode return false.
        CostCode cc = obj as CostCode;
        if (cc == null)
        {
            return false;
        }

        // Return true if the Id fields match:
        return Id == cc.Id;
    }

    public override int GetHashCode()
    {
        return Id; // If Id is int...
    }
}