当文本框的绑定设置为 OneWayToSource 时,WPF TargetNullValue 返回值
我将此 xaml 文本框
<TextBox Text="{Binding ProdFilter.Min, Mode=OneWayToSource,
UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}"
Width="50" DockPanel.Dock="Right" TabIndex="3" />
绑定到此属性:
public double? Min
{
get { return min; }
set
{
if (value == null)
value = 0;
min = value;
OnPropertyChanged("Min");
}
}
我遇到的问题是,当程序启动或用户清除文本时,文本框的文本设置为“0”。我不知道这种行为是否正确,因为我正在使用 OneWayToSource,但我希望当文本为空时将我的属性设置为 null(并且文本保持为空!)
有什么想法吗?谢谢!
I have this xaml textbox
<TextBox Text="{Binding ProdFilter.Min, Mode=OneWayToSource,
UpdateSourceTrigger=PropertyChanged, TargetNullValue=''}"
Width="50" DockPanel.Dock="Right" TabIndex="3" />
binded to this this property:
public double? Min
{
get { return min; }
set
{
if (value == null)
value = 0;
min = value;
OnPropertyChanged("Min");
}
}
The problem I have is that when the program starts or when there user clears the text, the textbox's text is set to "0". I don't know if this behaviour is right, because i'm using OneWayToSource, but i'd like my property to be set to null when text is empty (and the text to remain empty!)
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 WPF 在设置属性后会重新读取该值,即使绑定是
OneWayToSource
。请参阅此问题的答案,了解可能的解决方法。This is because WPF re-reads the value from the property after it sets it even though the binding is
OneWayToSource
. Please see the answer to this question for possible workaround.