WPF使用MVVM绑定到Popup控件中的ComboBox

时间:2014-01-02 22:42:13

标签: c# wpf mvvm combobox

我遇到了一个ComboBox的问题,当加载弹出控件时,它没有显示SelectedItem。我正在使用MVVM,因此除了Initialization之外没有任何代码。我对编程很新,所以我希望我能错过一些简单的东西。但是,我有其他ComboBoxes可以正常使用我在这些设置上的相同设置,所以我是perpelxed。我想知道Popup控件是否有不同之处,或者我需要考虑使用多个View Models。

我在Popup用户控件中的ComboBox的标记代码如下:

             <ComboBox Name="comboBox1"
              Height="23"
              Margin="5,280,0,0"
              HorizontalAlignment="Left"
              VerticalAlignment="Top"
              IsEnabled="{Binding PaymentScheduleBaseUnitEnabled}"
              ItemsSource="{Binding Path=ScheduleBaseUnitList,
                                    Mode=OneWay}"
              SelectedItem="{Binding Path=SelectedBaseUnitPaymentFrequencyChoice,
                                     Mode=TwoWay}"
              Style="{StaticResource ComboBoxShort}" />

以下是我的View Model中带有绑定属性的代码。

 public ObservableCollection<ScheduleBaseUnit> ScheduleBaseUnitList
    {
        get
        {
            _scheduleBaseUnitList = _utilityRepository.GetScheduleBaseUnitList();

            return _scheduleBaseUnitList;
        }
        set
        {
            _scheduleBaseUnitList = value;
            OnPropertyChanged("ScheduleBaseUnitList");

        }
    }

和SelectedBaseUnitPaymentFrequencyChoice属性。

  public ScheduleBaseUnit SelectedBaseUnitPaymentFrequencyChoice
    {
        get
        {
             if (_selectedBaseUnitPaymentFrequencyChoice != null)
            {
                return
                    _scheduleBaseUnitList.FirstOrDefault(
                        c => c.ScheduleBaseUnitId == _selectedBaseUnitPaymentFrequencyChoice.ScheduleBaseUnitId);
            }

        }
        set
        {
            if (_paymentFrequencyCustomer != null)
            {
                _paymentFrequencyCustomer.ScheduleBaseUnit = value;
                OnPropertyChanged("SelectedBaseUnitPaymentFrequencyChoice");
                OnPropertyChanged("TestTwo");
            }
        }
    }

这是ScheduleBaseUnit类

public class ScheduleBaseUnit : INotifyPropertyChanged
{
    public int ScheduleBaseUnitId { get; set; }

    public string Description { get; set; }

    public ScheduleBaseUnit(int ScheduleBaseUnitId, string Description)
    {
        this.ScheduleBaseUnitId = ScheduleBaseUnitId;
        this.Description = Description;
    }

    public ScheduleBaseUnit()
    {
    }

    public override string ToString()
    {
        return this.Description;
    }

    public override bool Equals(object obj)
    {
        return base.Equals(obj);
    }

    public bool Equals(ScheduleBaseUnit sbUnit)
    {
        if (sbUnit == null)
            return false;
        //return true if the fields match
        return sbUnit.ScheduleBaseUnitId == this.ScheduleBaseUnitId;
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

属性将很好地绑定到文本框,但不能绑定到ComboBox。我真的对这个感到困惑。非常感谢大家的帮助。

2 个答案:

答案 0 :(得分:0)

我的第一个想法是你实际上没有从你的Equals覆盖中调用强类型Equals方法:

public override bool Equals(object obj)
{
    return this.Equals(obj as ScheduleBaseUnit);
}

作为最佳实践,您还应该为GetHashCode添加更有用的覆盖,例如:

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

如果这些都不起作用,您确定所选项目是否在ItemsSource集合中吗?

答案 1 :(得分:0)

你没有在任何地方设置_selectedBaseUnitPaymentFrequencyChoice。同样在SelectedBaseUnitPaymentFrequencyChoice的setter中检查paymentFrequencyCustomer是否为null,如果为null,则不会引发更改的属性。你需要更多地考虑这个属性。