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