所以我得到了以下 ComboBox,其中 SelectedValue 绑定(bind)到下面的属性。使用以下绑定(bind),当我设置值时,绑定(bind)/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));
}
}
我很确定我以前曾经这样做过。我错过了什么?
最佳答案
我的心灵力量表明 PropertyChanged
事件正在尝试设置属性值。
setter 应该防止值未更改的情况。即-
set
{
if (_wireType != value) // or the appropriate comparison for your specific case
{
_wireType = value;
RaisePropertyChanged();
}
}
当然,堆栈跟踪可以确认实际发生的情况。
关于c# - RaisePropertyChanged 在 TwoWay Bound 属性上抛出 StackOverflow 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32469667/