简体   繁体   English

在WPF C#MVVM中将现有对象绑定到组合框

[英]Bind an Existing Object to a ComboBox in WPF C# MVVM

I am attempting to use a ComboBox to bind to a list of CostCodes . 我正在尝试使用ComboBox绑定到CostCodes列表。 A record named SelectedRecord is passed in to be edited and I want two things to happen. 名为SelectedRecord的记录被传递以进行编辑,我希望发生两件事。 Firstly, I want the ComboBox to select the CostCode that has already been applied to the SelectedRecord . 首先,我希望ComboBox选择已经应用于SelectedRecordCostCode I then want the user to be able to edit the CostCode so that they can assign the SelectedRecord to a different one. 然后,我希望用户能够编辑CostCode以便他们可以将SelectedRecord分配给另一个。

I have successfully bound my ComboBox to an ObservableCollection<CostCode> , however when the user loads the Window the CostCode that is already assigned to the SelectedRecord is not selected automatically in the ComboBox . 我已经成功将ComboBox绑定到ObservableCollection<CostCode> ,但是当用户加载Window ,不会自动在ComboBox选择已经分配给SelectedRecordCostCode Here is the XAML: 这是XAML:

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

I've tried using SelectedValue and SelectedValuePath but no luck. 我试过使用SelectedValueSelectedValuePath但没有运气。 Although the user is able to edit and the CostCodes are displayed correctly when the user clicks on the ComboBox , the CostCode already assigned to the SelectedRecord is not displayed. 虽然用户能够编辑和CostCodes当用户点击正确显示ComboBox时, CostCode已分配给SelectedRecord不显示。 I Included a couple of TextBoxes to show how I am binding to those, I effectively want to do exactly the same with their Text properties but with the ComboBox instead. 我包括了几个TextBoxes来展示我如何绑定到这些Text ComboBox ,我实际上想对它们的Text属性做完全相同的操作,但是要对ComboBox做相同的操作。

EDIT: CostCode.cs 编辑: 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 is chosen in a DataGrid of PhoneRecords then passed to an EditWindow where I want to set the applicable CostCode . PhoneRecordsDataGrid中选择SelectedRecord ,然后将其传递到EditWindow ,在其中我要设置适用的CostCode

I think you need to use SelectedItem . 我认为您需要使用SelectedItem
Something like this: 像这样:

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

Edit: Just as Spongebrot and Ed Plunkett says in the comments: If it is not the same instance of the CodeCode-object then you need to override the Equals method the the CostCode object. 编辑:正如Spongebrot和Ed Plunkett在评论中所说:如果不是CodeCode对象的相同实例,那么您需要覆盖CostCode对象的Equals方法。
(In this edit I also chaned the binding from "SelectedRecord" to "SelectedRecord.CostCode" based on comments in your question.) (在此编辑中,我还根据您问题的注释将绑定从“ SelectedRecord”更改为“ SelectedRecord.CostCode”。)

Edit 2: You can read about overriding Equals at MSDN . 编辑2:您可以在MSDN上阅读有关覆盖等于的信息。
In your case I think it should be like this: 在您的情况下,我认为应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM