简体   繁体   中英

ComboBox not updating display value after change

I have the following ComboBox on a tab:

<ComboBox Name="EmpNoRuleListBox"
          IsReadOnly="True"
          ItemsSource="{Binding AdjustmentSettings.EmpNoRuleCollection}" 
          DisplayMemberPath="Description"
          SelectedItem="{Binding AdjustmentSettings.SelectedEmpNoRule, Mode=TwoWay}" 
          IsSynchronizedWithCurrentItem="True" 
          HorizontalAlignment="Left"
          Width="300" />

And the AdjustmentSettings model is:

public class AdjustmentSettingsModel : ModelBase<AdjustmentSettingsModel>
{
    public String Company { get; set; }
    public Boolean ReloadEmployeeData { get; set; }
    public Boolean SortByName { get; set; }
    public Boolean ApplyJustificationRules { get; set; }
    public Int32 SeedNumber { get; set; }
    public Boolean ScanForMismatchedCodes { get; set; }
    public Boolean ReloadHRTables { get; set; }

    public EmpNoRule SelectedEmpNoRule
    {
        get { return _SelectedEmpNoRule; }
        set
        {
            if (_SelectedEmpNoRule != value)
            {
                _SelectedEmpNoRule = value;
                NotifyPropertyChanged(m => m.SelectedEmpNoRule);
            }
        }
    }
    private EmpNoRule _SelectedEmpNoRule;

    public EmpNoRuleCollection EmpNoRuleCollection { get; set; }
}

public class EmpNoRuleCollection : ObservableCollection<EmpNoRule>, INotifyPropertyChanged
{
      . . . 
}


public class EmpNoRule : ModelBase<EmpNoRule>
{
    #region Initialization & Cleanup

    // Overriding the GetHashCode prevents the Clone operation from marking an 
    // object Dirty when it is first cloned
    public override int GetHashCode()
    {
        return GetHashCode(this);
    }
    private int GetHashCode(object item, params string[] excludeProps)
    {
        int hashCode = 0;
        foreach (var prop in item.GetType().GetProperties())
        {
            if (!excludeProps.Contains(prop.Name))
            {
                object propVal = prop.GetValue(item, null);
                if (propVal != null)
                {
                    hashCode = hashCode ^ propVal.GetHashCode();
                }
            }
        }
        return hashCode;
    }


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

        // If parameter cannot be cast to EmpNoRule return false.
        EmpNoRule p = obj as EmpNoRule;
        if ((System.Object)p == null)
        {
            return false;
        }

        // Return true if the fields match:
        return (obj == p);

    }



    public EmpNoRule() { }

    #endregion Initialization & Cleanup

    /// <summary>
    /// Description
    /// </summary>
    [XmlElement("Description")]
    public String Description
    {
        get
        {
            return _Description;
        }
        set
        {
            if (_Description != value)
            {
                this._Description = value;
                NotifyPropertyChanged(m => m.Description);
            }
        }
    }
    private String _Description = "";


    /// <summary>
    /// Formula
    /// </summary>
    [XmlElement("Formula")]
    public String Formula
    {
        get
        {
            return _Formula;
        }
        set
        {
            if (_Formula != value)
            {
                this._Formula = value;
                NotifyPropertyChanged(m => m.Formula);
            }
        }
    }
    private String _Formula = "";
}

Everything works correctly EXCEPT the displayed value in the combobox does not change when I select a different EmpNoRule. The value of SelectedEmpNoRule is correctly set/updated and if I change to another tab and then come back to this one, the drop down value is updated.

Your code does not show how you update EmpNoRuleCollection property, but I assume you simply reassigning a new collection to this property. That will not update the combobox unless you publish a PropertyChanged event on "EmpNoRuleCollection".

I believe the reason why changing tab will update the combobox because the binding pulls the values from your EmpNoRuleCollection collection again when you switch tab.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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