【问题标题】:WPF binding to ComboBox within Popup control using MVVM使用 MVVM 将 WPF 绑定到 Popup 控件中的 ComboBox
【发布时间】:2014-01-02 22:42:13
【问题描述】:

我遇到了一个 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。我真的对此感到困惑。非常感谢大家的帮助。

【问题讨论】:

  • 您是否检查了输出窗口中的任何绑定错误?
  • 感谢您的建议。我检查了输出窗口,没有绑定错误。

标签: c# wpf mvvm combobox


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 感谢tencntraze,我已经进行了这两项更改。你是对的,我的 Equals 方法没有被调用,这当然是问题之一。不过,这显然不是我唯一的问题,因为绑定仍然无法正常工作。
  • @MoZahid 也是正确的,因为您需要更仔细地查看 SelectedBaseUnitPaymentFrequencyChocie,因为很难遵循它正在尝试做的事情并且 get/set 并不真正匹配。另一种可能性是您在设置 SelectedBaseUnitPaymentFrequencyChoice 之后设置了 ScheduleBaseUnitList 属性,因此选择不会被选中,因为 ItemsSource 中没有匹配的项目
  • 非常感谢。我必须重写 Equals,更改属性,并确保我的 Mediator 类通过视图模型之间的值。为了简洁起见,我没有将中介代码放在我的问题中......事实证明这是一个糟糕的选择。谢谢。
【解决方案2】:

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

【讨论】:

  • 你能考虑用一些缩进格式化你的代码吗?
猜你喜欢
  • 1970-01-01
  • 2011-02-06
  • 2019-06-23
  • 1970-01-01
  • 2019-10-11
  • 1970-01-01
  • 2011-09-02
  • 1970-01-01
  • 2011-10-28
相关资源
最近更新 更多