簡體   English   中英

組合框更改后不更新顯示值

[英]ComboBox not updating display value after change

我在選項卡上有以下ComboBox:

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

AdjustmentSettings模型為:

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 = "";
}

一切工作正常,但當我選擇其他EmpNoRule時,組合框中的顯示值不會更改。 正確設置/更新了SelectedEmpNoRule的值,並且如果我切換到另一個選項卡然后又返回到該選項卡,則下拉值將被更新。

您的代碼未顯示如何更新EmpNoRuleCollection屬性,但我假設您只是將新集合重新分配給該屬性。 除非您在“ EmpNoRuleCollection”上發布PropertyChanged事件,否則不會更新組合框。

我相信更改選項卡將更新組合框的原因是,當您切換選項卡時,綁定會再次從EmpNoRuleCollection集合中提取值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM