繁体   English   中英

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

[英]WPF binding to ComboBox within Popup control using MVVM

我遇到一个ComboBox的问题,该ComboBox在加载弹出控件时不显示SelectedItem。 我正在使用MVVM,因此除初始化外没有任何代码。 我是编程的新手,所以我希望我没有容易的事情。 但是,我还有其他ComboBoxes可以在这些设置上正常工作,因此我很佩服。 我想知道Popup控件是否可能有所不同,或者我需要考虑使用多个视图模型。

我在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}" />

这是我的视图模型中具有绑定属性的代码。

 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。 我对此确实感到困惑。 非常感谢大家的帮助。

我首先想到的是,您实际上并没有从Equals覆盖中调用强类型的Equals方法:

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

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

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

如果这些都不起作用,那么您确定所选项目位于ItemsSource集合中吗?

您未在任何地方设置_selectedBaseUnitPaymentFrequencyChoice。 同样在SelectedBaseUnitPaymentFrequencyChoice的设置器中,您检查paymentFrequencyCustomer是否不为null,如果为null,则不会引发更改的属性。 您需要对此属性进行更多考虑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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