gpt4 book ai didi

c# - WPF 绑定(bind)不能正确处理 int 类型的属性

转载 作者:太空狗 更新时间:2023-10-29 17:28:05 25 4
gpt4 key购买 nike

我的 View 模型中有一个 int 类型的属性,它绑定(bind)到一个 TextBox。一切正常,TwoWay 绑定(bind)工作正常,除了一种情况 -

如果我清除 TextBox 的值,则不会调用属性 setter ,尽管在 TextBox 中清除了值,属性仍保留以前的值。

有人遇到过类似的问题吗?有什么解决方法吗?

这是属性 -

public int MaxOccurrences
{
get
{
return this.maxOccurrences;
}
set
{
if (this.maxOccurrences != value)
{
this.maxOccurrences = value;
base.RaisePropertyChanged("MaxOccurrences");
}
}
}

这是我在 xaml 中绑定(bind)属性的方式 -

<TextBox Text="{Binding Path=MaxOccurrences, Mode=TwoWay, 
NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Center" Width="30" Margin="0,0,5,0"/>

最佳答案

我遇到了类似的问题。

您只需将代码更新为:

<TextBox Text="{Binding Path=MaxOccurrences, Mode=TwoWay, TargetNullValue={x:Static sys:String.Empty},
NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Center" Width="30" Margin="0,0,5,0"/>

关于c# - WPF 绑定(bind)不能正确处理 int 类型的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1199149/

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