gpt4 book ai didi

c# - RaisePropertyChanged 在双向绑定(bind)属性上抛出 StackOverflow 异常

转载 作者:太空宇宙 更新时间:2023-11-03 10:23:33 25 4
gpt4 key购买 nike

所以我得到了以下组合框,其中 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 在双向绑定(bind)属性上抛出 StackOverflow 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32469667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com