【问题标题】:RaisePropertyChanged throwing a StackOverflow Exception on TwoWay Bound propertyRaisePropertyChanged 在 TwoWay Bound 属性上引发 StackOverflow 异常
【发布时间】:2015-09-09 01:41:18
【问题描述】:

所以我得到了下面的组合框,其中 SelectedValue 绑定到下面的属性。使用以下绑定,当我设置值时,绑定/RaisePropertyChanged 组合会引发 StackOverflow 异常。

这是组合框

<ComboBox x:Name="WireType" ItemsSource="{x:Bind ViewModel.WireTypes}" SelectedValue="{x:Bind ViewModel.WireType, Mode=TwoWay}"/>

这里是属性

public string WireType
{
    get
    {
        return _wireType;
    }
    set
    {
        _wireType = value;
        RaisePropertyChanged();
    }
}

这是 RaisePropertyChanged 方法。

private void RaisePropertyChanged([CallerMemberName] string caller = "")
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(caller));
    }
}

我很确定我以前做过。我错过了什么?

【问题讨论】:

  • (某些)堆栈跟踪可能有用。

标签: c# wpf data-binding


【解决方案1】:

我的精神力量表明PropertyChanged 事件正在尝试设置属性值。

setter 应该防止值没有改变的情况。即-

set
{
    if (_wireType != value) // or the appropriate comparison for your specific case
    {
        _wireType = value;
        RaisePropertyChanged();
    }
}

当然,堆栈跟踪会确​​认实际发生的情况。

【讨论】:

  • 嗯,这对我来说是一个无聊的时刻 :) 感谢您的快速回复。像魅力一样工作。
【解决方案2】:

试试这个

public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string caller = "")
{
  if (this.PropertyChanged != null)
    this.PropertyChanged(this, new PropertyChangedEventArgs(caller));
}

【讨论】:

  • 莫希特。抱歉,我错过了发布活动。我在代码中已经有了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 2012-08-30
  • 1970-01-01
相关资源
最近更新 更多