Why is WPF ComboBox item not updating?

2020-06-29 06:19发布

问题:

I am not understanding why in my following example the "Billing Model" combobox doesn't display the property BillingModel.BillingModelDescription when the textbox does. After selection of a client, i want the combobox to display the current billng model description, but it stays blank. The textbox bound to the same thing does show the description. I have a collection of possible models as the ItemsSource, which is working fine. How do i update the Billing Model combobox upon selection of a client?

Here's the XAML:

<Window x:Class="WpfApplication7.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label Content="Client"/>
    <ComboBox ItemsSource="{Binding AllClientData}" DisplayMemberPath="EmployerStr"
              SelectedItem="{Binding SelectedClient}"
              Width="300"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label Content="Billing Model:"/>
    <ComboBox ItemsSource="{Binding AllBillingModels}" DisplayMemberPath="BillingModelDescription"
              SelectedItem="{Binding SelectedClient.BillingModel}"
              Width="300"/>
    </StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label Content="Billing Model" />
    <TextBox Text="{Binding SelectedClient.BillingModel.BillingModelDescription}" Width="200"/>
    </StackPanel>
</StackPanel>

And the code-behind (this is just an example, I am using MVVM etc in the full app, but this serves my puurpose to illustrate the issue):

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        AllClientData = new ObservableCollection<ClientRate>();
        AllBillingModels = new ObservableCollection<BillingModelType>();

        ClientRate uno = new ClientRate();
        uno.BillingModel = new BillingModelType();
        uno.BillingModel.BillingModelID = 3;
        uno.BillingModel.BillingModelDescription = "Free";
        uno.ID = 01;
        uno.EmployerName = "Employer1";

        ClientRate dos = new ClientRate();
        dos.BillingModel = new BillingModelType();
        dos.BillingModel.BillingModelID = 2;
        dos.BillingModel.BillingModelDescription = "Variable";
        dos.ID = 02;
        dos.EmployerName = "Employer2";

        ClientRate tre = new ClientRate();
        tre.BillingModel = new BillingModelType();
        tre.BillingModel.BillingModelID = 1;
        tre.BillingModel.BillingModelDescription = "Flat";
        tre.ID = 01;
        tre.EmployerName = "Employer3";

        AllClientData.Add(uno);
        AllClientData.Add(dos);
        AllClientData.Add(tre);

        BillingModelType one = new BillingModelType();
        one.BillingModelID = 1;
        one.BillingModelDescription = "Flat";

        BillingModelType two = new BillingModelType();
        two.BillingModelID = 2;
        two.BillingModelDescription = "Variable";

        BillingModelType three = new BillingModelType();
        three.BillingModelID = 3;
        three.BillingModelDescription = "Free";

        AllBillingModels.Add(one);
        AllBillingModels.Add(two);
        AllBillingModels.Add(three);

        InitializeComponent();
        this.DataContext = this;
    }

    private ObservableCollection<ClientRate> _allClientData;
    public ObservableCollection<ClientRate> AllClientData
    {
        get { return _allClientData; }
        set
        {
            if (_allClientData != value)
            {
                _allClientData = value;
                FirePropertyChanged("AllClientData");
            }
        }
    }

    private ClientRate _selectedClient;
    /// <summary>
    /// Gets/Sets Global SelectedClient object
    /// </summary>
    public ClientRate SelectedClient
    {
        get { return _selectedClient; }
        set
        {
            if (_selectedClient != value)
            {
                _selectedClient = value;
                FirePropertyChanged("SelectedClient");
            }
        }
    }

    //private BillingModelType _selectedBillingModel;
    //public BillingModelType SelectedBillingModel
    //{
    //    get
    //    {
    //        return _selectedBillingModel;
    //    }
    //    set
    //    {
    //        if (_selectedBillingModel != value)
    //        {
    //            _selectedBillingModel = value;
    //            FirePropertyChanged("SelectedBillingModel");
    //        }
    //    }
    //}

    private ObservableCollection<BillingModelType> _allBillingModels;
    /// <summary>
    /// Holds all possible billing model types 
    /// </summary>
    public ObservableCollection<BillingModelType> AllBillingModels
    {
        get { return _allBillingModels; }
        set
        {
            if (_allBillingModels != value)
            {
                _allBillingModels = value;
                FirePropertyChanged("AllBillingModels");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void FirePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }
}

public class BillingModelType
{
    /// <summary>
    /// Billing Model ID
    /// </summary>
    public int? BillingModelID { get; set; }
    /// <summary>
    /// Billing Model Description
    /// </summary>
    public string BillingModelDescription { get; set; }
}

public class ClientRate : INotifyPropertyChanged
{
    /// <summary>
    /// Employer name with Employer ID in parentheses
    /// </summary>
    public string EmployerStr { get { return EmployerName + " (" + ID + ")"; } }
    /// <summary>
    /// Employer ID
    /// </summary>
    public int? ID { get; set; }
    private string _EmployerName;
    /// <summary>
    /// Employer Official Name
    /// </summary>
    public string EmployerName
    {
        get { return _EmployerName; }
        set
        {
            if (_EmployerName != value)
            {
                _EmployerName = value;
                FirePropertyChanged("EmployerName");
            }
        }
    }

    private BillingModelType _billingModel;
    /// <summary>
    /// Rate Type  ID and Description
    /// </summary>
    public BillingModelType BillingModel
    {
        get { return _billingModel; }
        set
        {
            if (_billingModel != value)
            {
                _billingModel = value;
                FirePropertyChanged("BillingModel");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void FirePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }
}

回答1:

I fixed this problem by overriding Equals() in my BillingModelType class. The problem was, as I suspected, that the BillingModel was not the exact same instance of BillingModel used in the list of possible selections. So I simply added:

public override bool Equals(object obj)
    {
        if (obj == null || !(obj is BillingModelType))
            return false;
        return ((BillingModelType)obj).BillingModelID == this.BillingModelID;
    }

public override int GetHashCode()
    {
        return this.BillingModelID.GetHashCode();
    }

to the class for BillingModelTypes, and all is well. Credit to Rachel Lim, as i found her blog here about this problem: http://rachel53461.wordpress.com/2011/08/20/comboboxs-selecteditem-not-displaying/#comments



回答2:

I'm not sure what did you expect to happen, but you add to AllBillingModels all of the billing models (one two three), and they are all shown in the combobox (Flat, variable and free)

EDIT:

OK, if you are still here, I've got a nice idea
For your ComboBox Xaml:

<StackPanel Orientation="Horizontal">
        <Label Content="Billing Model:"/>
        <ComboBox ItemsSource="{Binding AllBillingModels}" DisplayMemberPath="BillingModelDescription"
          SelectedItem="{Binding SelectedClient.BillingModel}"
                  Text="{Binding SelectedClient.BillingModel.BillingModelDescription}"
          Width="300"/>
</StackPanel>

This updates the test in the combobox.