Binding中的TargetNullValue属性(项目)
Wilma项目中的例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <TextBox Width="28" 2 Margin="15,5,0,5" 3 HorizontalAlignment="Left" 4 VerticalContentAlignment="Center" 5 KeyboardNavigation.TabIndex="2" 6 FontFamily="/Halliburton.Wilma;component/Fonts/#Univers 55" 7 FontSize="10pt" 8 MaxLength="2" 9 Padding="2,3" 10 Style="{StaticResource Gray_TextBox_Style}" 11 Template="{StaticResource Base_TextBox_Template}" 12 Text="{Binding Path=CurTool.SleepDuration, 13 UpdateSourceTrigger=PropertyChanged, 14 TargetNullValue={x:Static sys:String.Empty}}" 15 local:Masking.Mask="^\d*$"> 16 <i:Interaction.Triggers> 17 <i:EventTrigger EventName="LostFocus"> 18 <prism:InvokeCommandAction Command="{Binding VerifySleepDuration}" /> 19 </i:EventTrigger> 20 </i:Interaction.Triggers> 21 </TextBox>
其中TargetNullValue和Binding的Path指定的后台property做对应,当后台property为null时,这个TextBox.Text={x:Static sys:String.Empty}表现为文档框里是的empty。相反如果文档框里是Empty的话后台的property就被set去‘null’
参考:
<TextBox Text="{Binding Total, TargetNullValue=0.00}" />
The above code will set the Total property to null if the value is entered is 0.00 and if Total property is null it will display 0.00. The same functionality can be achieved by checking for null in the property getter and setters, but this is very handy and you don't have to do it in all the places where you want to achieve this behaviour.
MSDN:http://msdn.microsoft.com/en-us/library/system.windows.data.bindingbase.targetnullvalue.aspx