【发布时间】:2016-07-23 23:21:32
【问题描述】:
TargetNullValue 应该在绑定 Source 评估为 null 时更新绑定 Target:
获取或设置当源值为空时在目标中使用的值。
除此之外,当Target 的值等于给定的TargetNullValue 时,它似乎还将Source 设置为null(如果可能)。换句话说,它有效地在null 和TargetNullValue 属性的值之间建立了等价关系。但是,文档中根本没有提到这一点。
看这个例子:
<Window x:Class="WPF_Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_Sandbox"
Title="MainWindow"
x:Name="ThisControl">
<StackPanel x:Name="MainStackPanel">
<TextBox x:Name="MyTextBox" Text="{Binding NullableInt, ElementName=ThisControl, TargetNullValue='', UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
</Window>
public partial class MainWindow : Window
{
private int? nullableInt;
public int? NullableInt
{
get { return nullableInt; }
set { nullableInt = value; }
}
public MainWindow()
{
InitializeComponent();
}
}
注意:UpdateSourcetrigger 只是为了让测试更容易而设置的,与有问题的效果无关。
如果您在NullableInt 的设置器中放置断点,您可以看到当您将TextBox 内容更改为'' 时,它会被触发(使用value == null)。
这是TargetNullValue 未记录的行为,还是这里有其他副作用?
编辑:
我偶然发现了这个话题,因为我在看这个问题:
Set value to null in WPF binding
【问题讨论】:
标签: c# wpf data-binding nullable